从mysql表中选择WHERE field='$array'? [英] Select from mysql table WHERE field='$array'?

查看:31
本文介绍了从mysql表中选择WHERE field='$array'?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有一系列的说法,一些用户的 ID.我怎么能做这样的事情:

If I have an array of say, some ID's of users. How could i do something like this:

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

有没有一种简单的方法可以做到这一点,我想过遍历数组项,然后构建一个大的WHERE -- OR -- OR -- OR"语句,但我认为这对于大型数组来说可能有点慢.

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.

推荐答案

使用IN:

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

您可以使用 implode(",", $array) 从数组中获取列表.

you can use implode(",", $array) to get the list together from the array.

这篇关于从mysql表中选择WHERE field='$array'?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆