MySQL 查询 table_b 中而不是 table_a 中的行

发布于 2024-09-08 13:27:10 字数 265 浏览 2 评论 0原文

我执行此查询是为了获取 table_b 中而不是 table_a 中的行:

SELECT table_b.* FROM table_b
LEFT JOIN table_a ON table_b.id = table_a.id
WHERE table_a.item_id IS NULL

这可以正常工作,但我需要另一个条件来应用于 table_a。我只需要比较 X 列的 ID 等于 3 的行,而不是比较整个表。有什么想法吗?

谢谢。

I'm doing this query to get rows that are in table_b and not in table_a:

SELECT table_b.* FROM table_b
LEFT JOIN table_a ON table_b.id = table_a.id
WHERE table_a.item_id IS NULL

This works ok but I need another condition to apply on table_a. I need to compare only the rows that got column X equal with the ID of 3, not to compare the whole table. Any ideas ?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

肩上的翅膀 2024-09-15 13:27:10

我不确定您是否正在寻找 table_a 列 x 或 table_b 列 x,但想法是将其添加到 join ON 子句中:

LEFT JOIN table_a ON table_b.id = table_a.id and table_a.id = 3

I'm not sure if you're looking for table_a column x or table_b column x, but the idea is to add that to the join ON clause:

LEFT JOIN table_a ON table_b.id = table_a.id and table_a.id = 3
奈何桥上唱咆哮 2024-09-15 13:27:10

试试这个...

SELECT table_b.* 
FROM table_b
LEFT JOIN table_a 
  ON table_b.id = table_a.id
  AND table_a.X = 3
WHERE table_a.item_id IS NULL

应该可以解决问题...

但这有点奇怪,因为它可能会返回比您需要的更多。此查询还将返回 table_b 中在 table_a 中确实有结果的任何行,但这些行的“X”值不为 3。

如果它不起作用,请尝试为您的数据提供一个示例需要查找您不想要的数据,我们可以尽力提供进一步帮助!

Try this out...

SELECT table_b.* 
FROM table_b
LEFT JOIN table_a 
  ON table_b.id = table_a.id
  AND table_a.X = 3
WHERE table_a.item_id IS NULL

Should do the trick...

But this is a bit strange, as it will probably return more than you require. This query will also return any rows from table_b which do have a result in table_a, but where those rows don't have an 'X' value of 3.

If it doesn't work, please try to give an example for data that you need to find, and data that you don't want, and we can try to help further!

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