I'm trying to grab content where id = 3 OR id = 9 OR id =开发者_高级运维 100... Keep in mind, I can have a few hundred of these ids.
What is the most efficient way to write my query?
$sql = "SELECT name FROM artists WHERE (id=3 OR id=9 OR .... id-100)"
....
WHERE id IN (3, 9, 100, ...)
You may want to use the IN()
function:
... WHERE id IN (3, 9, 100);
The IN()
function syntax:
expr IN (value,...)
Check whether a value is within a set of values ...
Returns
1
ifexpr
is equal to any of the values in theIN
list, else returns0
.
SELECT * FROM table WHERE id IN (1, 2, 3 etc)
And call only the columns you need rather than using *
!
You can use SQL IN condition
SELECT * FROM table WHERE ID IN ('1', '2', '3', '4');
SELECT * FROM table WHERE id IN (1,2,5)
SELECT * FROM table WHERE id IN (1,2,5);
$sql = "SELECT name FROM artists WHERE id REGEXP regex-pattern"
精彩评论