具有多个连接的查询将仅返回我的左连接之一

发布于 2024-10-03 08:14:10 字数 1124 浏览 5 评论 0原文

我在查询时遇到问题,问题是我从同一张表中连接了三次。

有问题的查询是这样的:

SELECT
      o.order_id, 
      n.title, 
      c.first_name,
      tdv5.tid,
      tdv6.name,
      tdv8.name AS settlement_month
 FROM orders o
      join products p           on o.product_id = p.nid
      join node n               on p.nid = n.nid
      join customers c          on o.customer_email = c.customer_email
      join term_node tn         on tn.nid = p.nid
      join term_data tdv6       on tn.tid = tdv6.tid AND tdv6.vid = 6
      left join term_data tdv5  on tn.tid = tdv5.tid AND tdv5.vid = 5
      left join term_data tdv8  on tn.tid = tdv8.tid AND tdv8.vid = 8

当我使用此查询时,只有使用 tn.tid 的第一个 JOIN 才会起作用。例如,如果最后三个连接中的唯一连接是这样的:

join term_data tdv6       on tn.tid = tdv6.tid AND tdv6.vid = 6

它将正常工作。 相同

left join term_data tdv5  on tn.tid = tdv5.tid AND tdv5.vid = 5

与And

left join term_data tdv8  on tn.tid = tdv8.tid AND tdv8.vid = 8

,但是当我将它们组合起来时,它们根本不起作用。当我组合三个查询时,只有 tdv6 有效。

您对问题有什么建议吗?谢谢。

I have trouble with a query, the problem is that i am left joining three times from the same table.

The query in question is this:

SELECT
      o.order_id, 
      n.title, 
      c.first_name,
      tdv5.tid,
      tdv6.name,
      tdv8.name AS settlement_month
 FROM orders o
      join products p           on o.product_id = p.nid
      join node n               on p.nid = n.nid
      join customers c          on o.customer_email = c.customer_email
      join term_node tn         on tn.nid = p.nid
      join term_data tdv6       on tn.tid = tdv6.tid AND tdv6.vid = 6
      left join term_data tdv5  on tn.tid = tdv5.tid AND tdv5.vid = 5
      left join term_data tdv8  on tn.tid = tdv8.tid AND tdv8.vid = 8

When i am using this query only the first JOIN using tn.tid will work. For an example if the only join of the last three is this:

join term_data tdv6       on tn.tid = tdv6.tid AND tdv6.vid = 6

It will work fine. The same with

left join term_data tdv5  on tn.tid = tdv5.tid AND tdv5.vid = 5

And

left join term_data tdv8  on tn.tid = tdv8.tid AND tdv8.vid = 8

However when i combine them, they won't work at all. Only tdv6 works when i combine the three queries.

Do you have suggestions on what is wrong? Thanks.

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

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

发布评论

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

评论(2

黑凤梨 2024-10-10 08:14:10

如果您尝试放置 WHERE 子句,如下所示:

SELECT
      o.order_id, 
      n.title, 
      c.first_name,
      tdv5.tid,
      tdv6.name,
      tdv8.name AS settlement_month
 FROM orders o
      join products p           on o.product_id = p.nid
      join node n               on p.nid = n.nid
      join customers c          on o.customer_email = c.customer_email
      join term_node tn         on tn.nid = p.nid
      join term_data tdv6       on tn.tid = tdv6.tid
      left join term_data tdv5  on tn.tid = tdv5.tid
      left join term_data tdv8  on tn.tid = tdv8.tid
WHERE
      tdv6.vid = 6 and
      tdv5.vid = 5 and
      tdv8.vid = 8

If you try putting a WHERE clause, like this:

SELECT
      o.order_id, 
      n.title, 
      c.first_name,
      tdv5.tid,
      tdv6.name,
      tdv8.name AS settlement_month
 FROM orders o
      join products p           on o.product_id = p.nid
      join node n               on p.nid = n.nid
      join customers c          on o.customer_email = c.customer_email
      join term_node tn         on tn.nid = p.nid
      join term_data tdv6       on tn.tid = tdv6.tid
      left join term_data tdv5  on tn.tid = tdv5.tid
      left join term_data tdv8  on tn.tid = tdv8.tid
WHERE
      tdv6.vid = 6 and
      tdv5.vid = 5 and
      tdv8.vid = 8
旧情勿念 2024-10-10 08:14:10

你确定你的结论是正确的吗?空值将出现在同一行,因此当 (5, 8) 中没有 term_data.tid== tn.tid 和 term_data.vid 的记录时,您只会获得以下记录: tdv6。这些记录的 tid 和/或结算月将为 NULL。

您是否也想离开加入 tdv6?现在是内部联接,这意味着您需要 tdv6 中的记录才能从 tdv5 或 tdv8 中获取具有相同 tid 的记录。

Are you sure your conclusions are correct? The null values will appear on the same row, so when there are no records where term_data.tid= = tn.tid and term_data.vid in (5, 8), you will only get the records for tdv6. tid and/or settlement_month will be NULL for those records.

Did you maybe mean to left join tdv6 as well? It is now an inner join, meaning that you need a record in tdv6 to get the records from tdv5 or tdv8 with the same tid.

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