php 从mysql表中选择WHERE field='$array'?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2382831/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Select from mysql table WHERE field='$array'?
提问by tarnfeld
If I have an array of say, some ID's of users. How could i do something like this:
如果我有一系列说法,一些用户 ID。我怎么能做这样的事情:
$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”语句,但我认为这对于大型数组来说可能有点慢。
回答by Pekka
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)从数组中获取列表。

