繁体   English   中英

如何在 LEFT JOIN 中使用 WHERE 条件?

[英]How to use WHERE condition with the LEFT JOIN?

如何在 LEFT_JOIN 中实现 WHERE 条件。 我的查询是:

SELECT t1.company_short_name, COUNT(t2.user_ip_address) as chart_accessed 
FROM tblcompanymaster t1 
LEFT JOIN tblhistorymaster t2 ON t1.row_id = t2.company_id 
WHERE t2.user_last_request_time BETWEEN 1603775329 AND 1606280929 
GROUP BY t1.company_short_name

我需要找到所有带有访问计数的 company_short_name,如果没有访问或计数为 0,那么它应该是 COUNT(t2.user_ip_address) 为 0。使用 where 条件只给出计数大于 1 的结果。我尝试了很多,但我无法修改代码。 任何建议都会有很大帮助

将您的 where 子句条件置于 ON 子句中

SELECT t1.company_short_name, COUNT(t2.user_ip_address) as chart_accessed 
FROM tblcompanymaster t1 
LEFT JOIN tblhistorymaster t2 ON t1.row_id = t2.company_id 
and t2.user_last_request_time BETWEEN 1603775329 AND 1606280929 
GROUP BY t1.company_short_name

您可以将过滤 (where) 组件添加到连接中 - 仅采用 t2 中存在的值。

SELECT t1.company_short_name, COUNT(t2.user_ip_address) as chart_accessed 
FROM tblcompanymaster t1 
LEFT JOIN tblhistorymaster t2 ON t1.row_id = t2.company_id AND t2.user_last_request_time BETWEEN 1603775329 AND 1606280929 
GROUP BY t1.company_short_name

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM