尝试使用左连接和分组进行查询并在 MySQL 上左表的字段上出现问题

发布于 2024-11-07 10:50:32 字数 1002 浏览 0 评论 0原文

我试图做一些查询,首先,我做了这个(并且有效):

SELECT RP.id, RP.product_name, RP.price, RP.retailer_id, RP.product_id, 
count(G.id) AS duration, G.active, RP.retprod_id, P.pr_id AS video 
FROM retailer_products AS RP 
LEFT JOIN groups G ON RP.id=G.retailer_product_id 
INNER JOIN products P ON P.id=RP.product_id 
WHERE (RP.product_id IN (1)) 
GROUP BY RP.id;

但是当我做这个时,它给了我一个空集,不同之处在于它在查询的末尾有一个“HAVING”字段可能是:0、1或NULL(由于LEFT JOIN,我没有链接到表retailer_product的组)

SELECT RP.id, RP.product_name, RP.price, RP.retailer_id, RP.product_id, 
count(G.id) AS duration, G.active, RP.retprod_id, P.pr_id AS video 
FROM retailer_products AS RP 
LEFT JOIN groups G ON RP.id=G.retailer_product_id 
INNER JOIN products P ON P.id=RP.product_id 
WHERE (RP.product_id IN (1)) 
GROUP BY RP.id HAVING G.active=1;

因此,我尝试了以下方法,但没有人工作:

-- HAVING G.active=1

-- HAVING G.active=1 OR G.active=NULL

-- HAVING G.active0

在MySQL上处理此问题的正确方法是什么?提前致谢!

Im trying to make some queries, first, I do this one (and works):

SELECT RP.id, RP.product_name, RP.price, RP.retailer_id, RP.product_id, 
count(G.id) AS duration, G.active, RP.retprod_id, P.pr_id AS video 
FROM retailer_products AS RP 
LEFT JOIN groups G ON RP.id=G.retailer_product_id 
INNER JOIN products P ON P.id=RP.product_id 
WHERE (RP.product_id IN (1)) 
GROUP BY RP.id;

But when I do this one, it gaves me an empty set, the difference is that it has a "HAVING" at the end of the query over a field that may be: 0, 1 or NULL (because of the LEFT JOIN, I have no groups linked to the table retailer_product)

SELECT RP.id, RP.product_name, RP.price, RP.retailer_id, RP.product_id, 
count(G.id) AS duration, G.active, RP.retprod_id, P.pr_id AS video 
FROM retailer_products AS RP 
LEFT JOIN groups G ON RP.id=G.retailer_product_id 
INNER JOIN products P ON P.id=RP.product_id 
WHERE (RP.product_id IN (1)) 
GROUP BY RP.id HAVING G.active=1;

So, I tried the following ways but no one works:

-- HAVING G.active=1

-- HAVING G.active=1 OR G.active=NULL

-- HAVING G.active0

What is the right way to handle this on MySQL? Thanks in advance!

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

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

发布评论

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

评论(2

云仙小弟 2024-11-14 10:50:32

将值与 NULL 进行比较的正确方法是 IS NULL。因此,您需要添加HAVING G.active IS NULL OR G.active=1。您的代码 G.active=NULL 始终为 NULL (false):

1 = NULL // NULL - false in conditions
NULL = NULL // NULL - false in conditions
NULL IS NULL // TRUE

The proper way to compare value with NULL is IS NULL. So you need to add HAVING G.active IS NULL OR G.active=1. Your code G.active=NULL is always NULL (false):

1 = NULL // NULL - false in conditions
NULL = NULL // NULL - false in conditions
NULL IS NULL // TRUE
望她远 2024-11-14 10:50:32

为什么不在“Group”ON 条件上添加 HAVING 条件?

SELECT RP.id, RP.product_name, RP.price, RP.retailer_id, RP.product_id, 
count(G.id) AS duration, G.active, RP.retprod_id, P.pr_id AS video 
FROM retailer_products AS RP 
LEFT JOIN groups G ON RP.id=G.retailer_product_id AND G.active=1
INNER JOIN products P ON P.id=RP.product_id 
WHERE (RP.product_id IN (1)) 
GROUP BY RP.id;

Why you don't add the HAVING condition on the "Group" ON condition?

SELECT RP.id, RP.product_name, RP.price, RP.retailer_id, RP.product_id, 
count(G.id) AS duration, G.active, RP.retprod_id, P.pr_id AS video 
FROM retailer_products AS RP 
LEFT JOIN groups G ON RP.id=G.retailer_product_id AND G.active=1
INNER JOIN products P ON P.id=RP.product_id 
WHERE (RP.product_id IN (1)) 
GROUP BY RP.id;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文