tags:

views:

48

answers:

3

How do I execute a query like this using PHP:

 SELECT [name] FROM [items] WHERE [ID]=1, [ID]=2, [ID]=3

and have MySQL return me all 3 rows?

 ID    name
 --    ----
 1     John
 2     Jane
 3     Jack
+5  A: 

Use:

SELECT name
  FROM ITEMS 
 WHERE id IN (1, 2, 3)
OMG Ponies
+1  A: 
$result = mysql_query("SELECT name FROM items WHERE id IN (1, 2, 3)");

while($row = mysql_fetch_array($result)) {
  echo $row['name'];
  echo "<br />";
}
mopoke
A: 

select [id], [name] from [items] where [id] in (1,2,3) order by [id]

SLoret
Don't use ordering unless needed. If no proper index is present, the `ORDER BY` query will lead to creation of temporary tables to execute the query.
Nirmal