开发者

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

开发者 https://www.devze.com 2023-02-14 04:47 出处:网络
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 ?

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

精彩评论

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