gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 14:57:28 25 4
gpt4 key购买 nike

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

有问题的查询是这样的:

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

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

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

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

最佳答案

如果您尝试放置 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

关于mysql - 具有多个连接的查询将仅返回我的左连接之一,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4225932/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com