WHERE id=preselected id's?

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
jmansa
Forum Commoner
Posts: 81
Joined: Wed Aug 23, 2006 4:00 am

WHERE id=preselected id's?

Post by jmansa »

I have a table with 8 users ( id= 1 - 8) and now I want the data for only id 1-4... How do I go about that? Tryid this... but something wrong!

Code: Select all

$result = mysql_query("SELECT * FROM electric2007 WHERE id=1-4 ORDER BY total desc")
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: WHERE id=preselected id's?

Post by John Cartwright »

Code: Select all

$result = mysql_query("SELECT * FROM electric2007 WHERE id IN (1,2,3,4) ORDER BY total desc")
is probably the simplest way
User avatar
aaronhall
DevNet Resident
Posts: 1040
Joined: Tue Aug 13, 2002 5:10 pm
Location: Back in Phoenix, missing the microbrews
Contact:

Post by aaronhall »

Just to be picky, but if the id column is the primary key, you can use "id <= 4"... it's a little cleaner to use comparison operators for integer types (probably faster for lookups on larger tables, too).
Post Reply