SQL查询中的数组? [英] Array in SQL Query?

查看:87
本文介绍了SQL查询中的数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用WHERE子句中的数组进行SQL查询时遇到问题.

i have a problem making a SQL Query with an array in my WHERE clause.

例如:

我的阵列:

$myarray[1] = "hi";
$myarray[2] = "there";
$myarray[3] = "everybody";

我的MySQL语句:

SELECT * FROM myTable WHERE title='".$myarray[]."'

有没有办法实现这一目标? 我是这样自己解决的:

Is there any way to realize that? I solved it myself like this:

for(...) {
$where = $where." title='".$myarray[$count]."' OR ";
}
$where = substr($where , 0, -3);
.....
SELECT * FROM myTable WHERE ".$where."

但是,如果我的数组中有成千上万个条目,那么SQL语句将太大且太慢,对吧?

But if i had thousands of entries in my array, the SQL Statement would be too big and slow, right?

谢谢

推荐答案

您可以使用mysql的

You can use mysql's IN-function

正如amosrevira所说,您需要对数组中的字符串进行转义.

As amosrevira said, you need to escape you strings in the array.

$myarray[1] = "'hi'";
$myarray[2] = "'there'";
$myarray[3] = "'everybody'";

$newarray = implode(", ", $myarray); //makes format 'hi', 'there', 'everybody' 

SELECT * FROM myTable WHERE title IN ($newarray);

这篇关于SQL查询中的数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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