左外部联接不会返回我的左表中的所有行吗?

编程入门 行业动态 更新时间:2024-10-25 10:24:51
本文介绍了左外部联接不会返回我的左表中的所有行吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试使用以下查询获取每天打开的页面数.

I am trying to get the number of page opens on a per day basis using the following query.

SELECT day.days, COUNT(*) as opens FROM day LEFT OUTER JOIN tracking ON day.days = DAY(FROM_UNIXTIME(open_date)) WHERE tracking.open_id = 10 GROUP BY day.days

我得到的输出是这样:

days opens 1 9 9 2

问题是,在我的日表中,我有一列包含数字1到30来表示一个月中的天数.我做了一个左外部连接,我希望在天列中显示整天!

The thing is, in my day table, I have a single column that contains the number 1 to 30 to represent the days in a month. I did a left outer join and I am expecting to have all days show on the days column!

但是我的查询正在这样做,为什么会这样呢?

But my query is doing that, why might that be?

谢谢大家的帮助.

推荐答案

Nanne给出的答案解释了为什么您没有得到期望的结果(您的WHERE子句删除了行),但没有解决该问题的方法.

Nanne's answer given explains why you don't get the desired result (your WHERE clause removes rows), but not how to fix it.

解决方案是将WHERE更改为AND,以便该条件成为联接条件的一部分,而不是联接之后应用的过滤器:

The solution is to change WHERE to AND so that the condition is part of the join condition, not a filter applied after the join:

SELECT day.days, COUNT(*) as opens FROM day LEFT OUTER JOIN tracking ON day.days = DAY(FROM_UNIXTIME(open_date)) AND tracking.open_id = 10 GROUP BY day.days

现在,左表中的所有行都将出现在结果中.

Now all rows in the left table will be present in the result.

更多推荐

左外部联接不会返回我的左表中的所有行吗?

本文发布于:2023-10-18 03:18:47,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1502918.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:左表中

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!