简体   繁体   English

从 mysql 表中选择 WHERE field='$array'?

[英]Select from mysql table WHERE field='$array'?

If I have an array of say, some ID's of users.如果我有一系列说法,一些用户 ID。 How could i do something like this:我怎么能做这样的事情:

$array = array(1,40,20,55,29,48);
$sql = "SELECT * FROM `myTable` WHERE `myField`='$array'";

Is there a simple way to do this, I thought about looping through array items and then building up one big "WHERE -- OR -- OR -- OR" statement but i thought that might be a bit slow for large arrays.有没有一种简单的方法可以做到这一点,我想过遍历数组项,然后构建一个大的“WHERE -- OR -- OR -- OR”语句,但我认为这对于大型数组来说可能有点慢。

Use IN :使用IN

$sql = "SELECT * FROM `myTable` WHERE `myField` IN (1,40,20,55,29,48)";

you can use implode(",", $array) to get the list together from the array.您可以使用implode(",", $array)从数组中获取列表。

You want to use IN :你想使用IN

WHERE `myfield` IN (1,40,20,55,29,48)

Use implode to construct the string:使用内构造字符串:

$sql = "SELECT * FROM `myTable` WHERE `myField` IN (" . implode(',', $array) . ")";

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

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