简体   繁体   English

如何在不使用“IN”的情况下在 MySQL 连接中使用字符串

[英]How to use a string in join of MySQL without using "IN"

I have an variable of order ids like:我有一个订单 ID 变量,例如:

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

I want to use this string with MySQL join, without using "IN" statement because "IN" statement take much time to execute and I think "IN" statement is not good for that kind of things.我想在 MySQL 连接中使用这个字符串,而不使用“IN”语句,因为“IN”语句需要很多时间来执行,我认为“IN”语句不适合那种事情。

Thanks in advance.提前致谢。

If you don't want to use IN you can use UNION:如果你不想使用 IN 你可以使用 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);

But I would use IN!但我会使用 IN!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM