If i have a table named products and its structure looked like this
id
product
price
How can I select only the rows whose id's are 3,6,10,13,15 ?
These values are dynamic though but will be provi开发者_高级运维ded.
Instead of doing select statement where id = '3' and id='6'
and so on is there another way of doing this?
Try:
select * from table where id in (3,6,10,13,15);
You can use the IN()
operator:
SELECT * FROM table WHERE id IN(3,6,10,13,15)
Assuming PHP...
$ids = array(3, 6, 10, 13, 15);
$query = 'SELECT *
FROM `table`
WHERE `id`
IN (' . implode(',', $ids) . ')';
select * from table where id in(3,6,10,13,15)
edit: looks like I was 15 seconds slow!
SELECT * FROM products WHERE id IN ( 3,6,10,13,15 )
精彩评论