mysql selecting multiple rows that match array of ids
To obtain multiple values from the where
clause, there is no need to use OR
. Use in
, like in the query below:
SELECT `key`, `value` FROM settings
WHERE `key` in ( :base_url, :google_analytics, :site_domain);
This is the same as the first:
SELECT * FROM users where id in (1,2,3,4,5);
So if you have an array of user ids, you must implode
it with the ','
glue characters.
Here's a complete example with php:
<?php
$users = array(1,2,3,4,5);
$usersStr = implode(',', $users); // returns 1,2,3,4,5
$sql = "SELECT * FROM users where id in ({$userStr})";
....