PHP MYSQL 连接查询。 SELECT WHERE AND IN OR 逻辑错误

发布于 2024-10-20 14:43:56 字数 1729 浏览 2 评论 0原文

嘿大家。我认为我的查询中有逻辑错误。输出是正确的,但是是三元组。我盯着这个看了很久,却没有看到。有人可以解释一下吗?谢谢!!

也想添加此信息。

  • $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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

溺孤伤于心 2024-10-27 14:43:56

您应该缩进并简化 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)
  )
");

You should indent and shortcut your SQL for better readability

$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)
  )
");
╰◇生如夏花灿烂 2024-10-27 14:43:56

如果我正确地得到了你的 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))");

If I got your SQL structure correctly, changing SQL like this should probably fix it. At least it's a proper usage.

$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))");
星軌x 2024-10-27 14:43:56

如果这对您有用,请提供反馈:

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));

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));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文