如何在mysql的join中使用字符串而不使用“in”

dxxyhpgq  于 2021-06-20  发布在  Mysql
关注(0)|答案(1)|浏览(377)

我有一个顺序变量,比如:

$OrderIds = "1,2,3,4,5,6"; //thousands of ids

我想在mysql join中使用这个字符串,而不使用“in”语句,因为“in”语句需要很多时间来执行,我认为“in”语句不适合这种情况。
提前谢谢。

rqqzpn5f

rqqzpn5f1#

如果不想在中使用,可以使用union:

$sql=""
$OrderIds=explode(',',$OrderIds);
for($i=0;$i++;$i<count($OrderIds)){
   $where.=" SELECT * FROM mytable WHERE orderId=".$OrderIds[$i]; //quote the id
   if($i<count($OrderIds)-1) $where.=" UNION ";
}
$db->query($sql);

但我会用在!

相关问题