tags:

views:

76

answers:

3

I have a table that has nine columns in it. one of them is the primary key.

How can I select a single row, and all of the columns in it, by the primary key or column 8 or 4?

I'm using PHP and MySQL

+3  A: 
select *
from MyTable
where MyPrimaryKey = 123
RedFilter
This works, thank you.
aggitan
+2  A: 

If I understand your question correctly:

select * from tbl where id = 123 or colname4 = 'x' or colname8 = 'y' limit 1

The 'limit' statement makes sure there is only one row returned.

ChristopheD
This will help me further on down the road, thank you!
aggitan
A: 

Columns in SQL don't have a defined 'order'. Database systems generally keep track of an order for display purposes, but it doesn't make sense to ask a database to select a column by number. You need to know the column's name in order to query its contents.

The same thing goes for the primary key (which, incidentally, may not be just a single column). You have to know which column it is, and what that column is named, in order to execute a query.

If you don't know these things, or need to figure them out dynamically, then

DESCRIBE tablename;

will tell you the names of each column, and whether it is part of the primary key or not. It will return a table that you can read, like any other result.

Ian Clelland