[英]PHP MySQL multiple conditions in query
I have a database with some records and columns: "type", "id" and others. 我有一个包含一些记录和列的数据库:“类型”,“ id”和其他。 I would like to select all records with any "type" that's mentioned in array $typearray but without records which id's are in $idarray.
我想选择数组$ typearray中提到的所有带有“类型”的记录,但没有$ idarray中有ID的记录。 I tried to do something like:
我试图做类似的事情:
$result = mysql_query("SELECT * FROM db
WHERE type IN('".implode("','",$typearray)."')
AND id NOT IN('".implode("','",$idarray)."')
ORDER BY date DESC LIMIT 10");
But it doesn't seem to work - the: 但这似乎不起作用-
type IN('".implode("','",$typearray)."')
condition works fine but the second does not. 条件正常,但第二个则不行。 At first I thought the issue was with column types - both arrays contains strings, "type" column is a VARCHAR(10) and id is INT, but changing it to string didn't help.
起初,我认为问题出在列类型上-两个数组都包含字符串,“ type”列是VARCHAR(10),id是INT,但是将其更改为string并没有帮助。 What am I doing wrong?
我究竟做错了什么?
EDIT: 编辑:
If I print a query I get this: 如果我打印查询,则会得到以下信息:
SELECT * FROM db
WHERE type IN('apple','tomato','potato')
AND id NOT IN('20','1','10','15','8')
ORDER BY date DESC LIMIT 10
The first condition works fine: it selects only apples, tomatoes and potatoes. 第一个条件很好:它仅选择苹果,西红柿和土豆。 The second condition does nothing and even if I type manually:
第二个条件不起作用,即使我手动键入也是如此:
id NOT IN('20','1','10','15','8')
or 要么
id NOT IN('20,1,10,15,8')
or 要么
id NOT IN(20,1,10,15,8)
it still fails. 它仍然失败。
EDIT2: 编辑2:
Actually forget it. 其实算了 I'm an idiot.
我是个白痴。 I've mixed up my variables a bit...
我混合了一些变量...
You should try this query, or at least do a echo $result, "\\n";
您应该尝试此查询,或者至少执行
echo $result, "\\n";
, into mysql workbench to see what it give. ,进入mysql工作台,看看它能提供什么。
And I'd say the default behavior to have when a query does not work: first in Mysql Workbench (or any SQL runner like Toad, Squirrel ...) 我会说当查询不起作用时的默认行为:首先在Mysql Workbench中(或在任何SQL运行器中,例如Toad,Squirrel ...)
$result = mysql_query("SELECT * FROM db
WHERE type IN('".implode("','",$typearray)."')
AND id NOT IN('".implode("','",$idarray)."')
ORDER BY date DESC LIMIT 10");
If $typearray
is empty, you are looking for type IN ('')
, which might never matches. 如果
$typearray
为空,则您正在寻找type IN ('')
,该类型可能永远不匹配。 If $idarray
is empty, you are looking for id not in ('')
which might always matches. 如果
$idarray
为空,则id not in ('')
中寻找可能总是匹配的id not in ('')
。
And if $typearray
or $idarray
contains invalid character, you will get an error. 而且,如果
$typearray
或$idarray
包含无效字符,则会出现错误。
[edit] by the way, if $typearray
or $idarray
may contains invalid character, you might use array_map and mysqli_escape_string : [编辑]顺便说一句,如果
$typearray
或$idarray
可能包含无效字符,则可以使用array_map和mysqli_escape_string :
$result = mysql_query("SELECT * FROM db
WHERE type IN('".implode("','", array_map('mysql_escape_string', $typearray))."')
AND id NOT IN('".implode("','", array_map('mysql_escape_string', $idarray))."')
ORDER BY date DESC LIMIT 10");
you don't need to escape integer variables: 您不需要转义整数变量:
...
AND id NOT IN(" . implode(",", $idarray).")
...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.