如何按 ID 序列使用 where 子句

how to use where clause by sequence of ids

我有一个由逗号合并的 ids 序列:

$ids = '1,2,3,4,5,6,7,8,9,10' ;
select * from ads WHERE id = $ids ...

现在如何通过这些 ids 获取 ads table 的内容?

使用IN运算符

试试这个:

SELECT * 
FROM ads 
WHERE id IN ($ids);

您正在使用 PHP。您可以像这样构建 SQL:

$ids = '1,2,3,4,5,6,7,8,9,10';
$sql = 'SELECT * FROM ads WHERE id IN ('.$ids.')';
//execute the query (don't use mysql_* function ;) )

从 MySQL 5.6 开始你也可以使用 FIND_IN_SET():

$ids = '1,2,3,4,5,6,7,8,9,10';
$sql = "SELECT * FROM ads WHERE FIND_IN_SET(id, '".$ids."')";
//execute the query (don't use mysql_* function ;) )