PHP MYSQL 连接查询。 SELECT WHERE AND IN OR 逻辑错误
嘿大家。我认为我的查询中有逻辑错误。输出是正确的,但是是三元组。我盯着这个看了很久,却没有看到。有人可以解释一下吗?谢谢!!
也想添加此信息。
- $userid = 1
- $UserIDAList = (1,1,6)
- $UserIDBList = (2,3,1)
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/>";
}
仅 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)))
输出:
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
编辑:这是我最终使用的。 (无法弄清楚这里的缩进。)
但现在我缺少数据库中的一个条目。
$结果 = mysql_query("
输出如下所示。
选择 cl.ContactID、cn.FirstName
FROM TBL_ContactName AS cn
INNER JOIN TBL_ContactsList AS cl
ON cl.ContactID = cn.NameID
哪里
cn.NameID != $userid
和(
cn.NameID IN ($UserIDBList) 或 cn.NameID IN ($UserIDAList)
)
”);
ID:2 姓名:乔
ID:3 姓名:Jimbo
但是当我输入 LEFT JOIN 时,我得到了这个。已关闭但仍缺少 ID。
ID:2 姓名:乔
ID:3 姓名:Jimbo
ID: 姓名: Mike
有什么想法吗?谢谢!
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!!
Also wanted to add this info as well.
- $userid = 1
- $UserIDAList = (1,1,6)
- $UserIDBList = (2,3,1)
PHP-Code:
$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):
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("
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)
)
");
The output looks like this.
ID: 2 Name: Joe
ID: 3 Name: 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
Any ideas?? THanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您应该缩进并简化 SQL 以获得更好的可读性
You should indent and shortcut your SQL for better readability
如果我正确地得到了你的 SQL 结构,像这样改变 SQL 应该可以解决它。至少这是正确的用法。
If I got your SQL structure correctly, changing SQL like this should probably fix it. At least it's a proper usage.
如果这对您有用,请提供反馈:
please give feedback, if this works out for you: