Mysql 组合SUM()、GROUP_BY()和LEFT_JOIN()会返回不正确的结果:如何修复?

Mysql 组合SUM()、GROUP_BY()和LEFT_JOIN()会返回不正确的结果:如何修复?,mysql,group-by,left-join,Mysql,Group By,Left Join,我正在编写一个查询,该查询应返回多个用户每天/每月/年的累计小时数 该表如下所示: +------------------------------------------+ | id | entity_id | minutes | person | date | +------------------------------------------+ select date_format(from_unixtime(date), '%Y-%m-%d') as myDate,

我正在编写一个查询,该查询应返回多个用户每天/每月/年的累计小时数

该表如下所示:

+------------------------------------------+
| id | entity_id | minutes | person | date |
+------------------------------------------+
select 
    date_format(from_unixtime(date), '%Y-%m-%d') as myDate,
    ROUND(SUM(time) / 60,1) as hours

from time h
输出的外观:

+----------------------------+
| year | month | day | hours |
| 2008 | 12    | 1   | 30    |
| 2008 | 12    | 2   | 40    |
| 2008 | 12    | 3   | 23    |
+----------------------------+
select 
    date_format(from_unixtime(date), '%Y-%m-%d') as myDate,
    ROUND(SUM(time) / 60,1) as hours

from time h

left join tag_entity te on te.entity_id = h.entity_id
left join tags t on t.tag_id = te.tag_id

where (t.tag_name NOT IN ('foo', 'bar', 'baz') OR t.tag_name IS NULL) 

group by
    myDate

order by
    hours DESC, myDate ASC
相反,
小时数
通常要多得多,这是由于
左连接
导致返回的行造成的

问题是我需要根据链接到相应实体的标记来查询这个表。当我连接两个表(
tag\u entity
提供链接和
tags
提供实际标记名)时,我的
SUM()
不再工作,因为返回的结果太多

查询:

+----------------------------+
| year | month | day | hours |
| 2008 | 12    | 1   | 30    |
| 2008 | 12    | 2   | 40    |
| 2008 | 12    | 3   | 23    |
+----------------------------+
select 
    date_format(from_unixtime(date), '%Y-%m-%d') as myDate,
    ROUND(SUM(time) / 60,1) as hours

from time h

left join tag_entity te on te.entity_id = h.entity_id
left join tags t on t.tag_id = te.tag_id

where (t.tag_name NOT IN ('foo', 'bar', 'baz') OR t.tag_name IS NULL) 

group by
    myDate

order by
    hours DESC, myDate ASC
我怎样才能解决这个问题

编辑:

以下是
标记
标记实体
的模式:

+-----------+---------+
| Field     | Type    |
+-----------+---------+
| id        | int(11) |
| tag_id    | int(11) |
| entity_id | int(11) |
+-----------+---------+
标签

+----------+-------------+
| Field    | Type        |
+----------+-------------+
| tag_id   | int(11)     |
| tag_name | varchar(50) |
+----------+-------------+
标记实体

+-----------+---------+
| Field     | Type    |
+-----------+---------+
| id        | int(11) |
| tag_id    | int(11) |
| entity_id | int(11) |
+-----------+---------+

也许你想要这样的东西:

+------------------------------------------+
| id | entity_id | minutes | person | date |
+------------------------------------------+
select 
    date_format(from_unixtime(date), '%Y-%m-%d') as myDate,
    ROUND(SUM(time) / 60,1) as hours

from time h
te.entity\u id=h.entity\u id上的左连接标记\u entity te


分组依据
结果进行分组,而不是单独对表行进行分组

根据您的注释,仅返回时间表中未链接到这些标记之一的行:


也许我还没有喝足够的咖啡,但我很难推断出你的模式。你能为标记和标记实体发布模式的相关部分吗?还有,“日期”和“时间”是在哪个表中定义的?@Andrew感谢您的回答。我添加了模式<代码>日期和时间在我的
时间
表中。您正在从t中选择
,并执行
左连接标记t
-语法错误。你忘记输入主表名了吗?@Marc oops。已发布的查询进行了一定程度的调整,并且在原始查询中不会出现此问题。将在本例中修复。您希望实体没有标记('foo','bar','baz')之一,还是希望实体包含除('foo','bar','baz')以外的标记?如果实体1有标记“foo”和标记“me”,是否应该包括它?除了将日期格式更改为一列之外,这解决不了任何问题。不过,为了简化查询,我将其更改为只返回一个日期列。您对其进行了测试吗?这不仅仅是日期格式的改变。您查询的问题是,您正在按所有年份的月份以及所有月份和年份的天数进行分组,例如,所有年份的所有一月的总和。它与左连接无关。除此之外,你不是说
和t.tag\u name不是空的吗
?是的,我测试了它,可惜没有结果。GROUPBY子句不应该按照您描述的方式工作:它组合了组参数。你的例子得到了完全相同的结果。哇,你确实做到了。非常感谢,你让我度过了一个周末。