mySQL union无法按预期工作

时间:2021-01-12 21:45:04

I have following SQL statementL:

我有以下SQL语句L:

select DATE(bla), count(*) from tableA group by DATE(bla)
UNION ALL
select DATE(bla), count(*) from tableB group by DATE(bla)
order by "DATE(bla)"

What I get from the first part of the query:

我从查询的第一部分得到的结果:

(2012-05-07,  13)

And from the second part:

从第二部分:

(2012-05-07,  15)

What I would expect to see after executing the whole query:

执行整个查询后我期望看到的内容:

[(2012-05-07,  13),
(2012-05-07,  15)]

What I really get:

我真正得到的:

[(2012-05-07,  13)]

The question is why and what should I change to get what I expect?

问题是为什么我应该改变什么以获得我期望的东西?

2 个解决方案

#1


3  

UNION removes duplicate rows, try UNION ALL:

UNION删除重复的行,尝试UNION ALL:

select DATE(bla), count(*) from tableA group by DATE(bla)
UNION ALL
select DATE(bla), count(*) from tableB group by DATE(bla)

#2


0  

OK, I solved the problem. You were right, the problem has something to do with the data. In the real case I had many rows but thought 'order by DATE(bla)' part of the query will sort them by date. This is not true, and this will be ignored until I add alias for the date field. And that's why I was so confused. Sorry for that, and thanks for your help.

好的,我解决了这个问题。你是对的,这个问题与数据有关。在实际情况下,我有很多行,但认为'按DATE(bla)命令'部分查询将按日期对它们进行排序。这不是真的,在我为日期字段添加别名之前,这将被忽略。这就是为什么我这么困惑。对不起,谢谢你的帮助。

#1


3  

UNION removes duplicate rows, try UNION ALL:

UNION删除重复的行,尝试UNION ALL:

select DATE(bla), count(*) from tableA group by DATE(bla)
UNION ALL
select DATE(bla), count(*) from tableB group by DATE(bla)

#2


0  

OK, I solved the problem. You were right, the problem has something to do with the data. In the real case I had many rows but thought 'order by DATE(bla)' part of the query will sort them by date. This is not true, and this will be ignored until I add alias for the date field. And that's why I was so confused. Sorry for that, and thanks for your help.

好的,我解决了这个问题。你是对的,这个问题与数据有关。在实际情况下,我有很多行,但认为'按DATE(bla)命令'部分查询将按日期对它们进行排序。这不是真的,在我为日期字段添加别名之前,这将被忽略。这就是为什么我这么困惑。对不起,谢谢你的帮助。