PHP MYSQL 加入查询。SELECT WHERE AND IN OR 逻辑错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5177728/
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
PHP MYSQL Join Query. SELECT WHERE AND IN OR Logic Error
提问by Newphper
Hey all. I think I have a logic error in my qry. The output is correct but in triplets. I've been staring at this for a long time and not seeing it. Can someone shed some light on this? Thanks!!
大家好。我想我的 qry 中有逻辑错误。输出是正确的,但在三元组中。我一直盯着这个很长时间,没有看到它。有人可以对此有所了解吗?谢谢!!
Also wanted to add this info as well.
也想添加此信息。
- $userid = 1
- $UserIDAList = (1,1,6)
- $UserIDBList = (2,3,1)
- $userid = 1
- $UserIDAList = (1,1,6)
- $UserIDBList = (2,3,1)
PHP-Code:
PHP-代码:
$result = mysql_query("SELECT TBL_ContactsList.ContactID, TBL_ContactName.FirstName FROM TBL_ContactsList, TBL_ContactName WHERE ((TBL_ContactName.NameID != $userid) AND (TBL_ContactsList.ContactID != $userid)) AND ((TBL_ContactName.NameID IN ($UserIDAList) OR TBL_ContactName.NameID IN $UserIDBList)))");
while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
printf ("ID: %s Name: %s", $row[0], $row[1]);
echo "<br/>";
}
Only the SQL-Query (readability):
只有 SQL 查询(可读性):
SELECT TBL_ContactsList.ContactID, TBL_ContactName.FirstName
FROM TBL_ContactsList, TBL_ContactName
WHERE ((TBL_ContactName.NameID != $userid) AND (TBL_ContactsList.ContactID != $userid))
AND ((TBL_ContactName.NameID IN ($UserIDAList) OR TBL_ContactName.NameID IN $UserIDBList)))
Output:
输出:
ID: 2 Name: Joe
ID: 3 Name: Joe
ID: 4 Name: Joe
ID: 2 Name: Jimbo
ID: 3 Name: Jimbo
ID: 4 Name: Jimbo
ID: 2 Name: Mike
ID: 3 Name: Mike
EDIT: Here is what I ended up using. (can't figure out indent on here.)
编辑:这是我最终使用的。(无法弄清楚这里的缩进。)
But now I am missing an entry from the db.
但现在我错过了数据库中的一个条目。
$result = mysql_query("
The output looks like this.
SELECT cl.ContactID, cn.FirstName
FROM TBL_ContactName AS cn
INNER JOIN TBL_ContactsList AS cl
ON cl.ContactID = cn.NameID
WHERE
cn.NameID != $userid
AND (
cn.NameID IN ($UserIDBList) OR cn.NameID IN ($UserIDAList)
)
");
ID: 2 Name: Joe
ID: 3 Name: Jimbo
$result = mysql_query("
输出看起来像这样。
SELECT cl.ContactID, cn.FirstName
FROM TBL_ContactName AS cn
INNER JOIN TBL_ContactsList AS cl
ON cl.ContactID = cn.NameID
WHERE
cn.NameID != $userid
AND (
cn.NameID IN ($UserIDBList) OR cn.NameID IN ($UserIDAList)
)
");
ID:2 姓名:Joe
ID:3 姓名:Jimbo
But when I put LEFT JOIN I get this. Close but still missing ID.
ID: 2 Name: Joe
ID: 3 Name: Jimbo
ID: Name: Mike
但是当我放 LEFT JOIN 时,我明白了。关闭但仍然缺少 ID。
ID:2 姓名:Joe
ID:3 姓名:Jimbo
ID:姓名:Mike
Any ideas?? THanks!
有任何想法吗??谢谢!
回答by Arda
If I got your SQL structure correctly, changing SQL like this should probably fix it. At least it's a proper usage.
如果我正确地得到了你的 SQL 结构,像这样改变 SQL 应该可以解决它。至少这是一个正确的用法。
$result=mysql_query("SELECT cl.ContactID, cn.FirstName FROM TBL_ContactsList cl INNER JOIN TBL_ContactName cn ON cn.NameID=cl.ContactID WHERE cn.NameID != $userid AND (CN.NameID IN ($UserIDAList) OR CL.NameID IN ($UserIDBList))");
回答by sod
You should indent and shortcut your SQL for better readability
您应该缩进和缩短 SQL 以获得更好的可读性
$result = mysql_query("
SELECT cl.ContactID, cl.FirstName
FROM TBL_ContactsList cl
JOIN TBL_ContactName cn
WHERE (
cn.NameID != $userid AND
cl.ContactID != $userid
) AND (
cn.NameID IN ($UserIDAList) OR
cn.NameID IN ($UserIDBList)
)
");
回答by Daniel Kutik
please give feedback, if this works out for you:
请提供反馈,如果这对您有用:
SELECT cl.ContactID, cn.FirstName
FROM TBL_ContactName AS cn
JOIN TBL_ContactsList as cl
ON cn.NameID = cn.ContactID
WHERE cn.NameID != $userid
AND (cn.NameID IN ($UserIDAList) OR cn.NameID IN ($UserIDBList));