开发者

How to select only the rows of provided table id's

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 )
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜