当我有多个WHERE子句时,我在UNION语句中使用Count(*)时遇到问题。
以下是我试图开始工作的MySQL语法:
SELECT Count(*)
FROM (SELECT title,
taskstatus,
Extract(year FROM finished) AS year,
Extract(month FROM finished) AS month
FROM todo_languages
UNION ALL
SELECT title,
taskstatus,
Extract(year FROM finished) AS year,
Extract(month FROM finished) AS month
FROM todo_main
UNION ALL
SELECT title,
taskstatus,
Extract(year FROM finished) AS year,
Extract(month FROM finished) AS month
FROM todo_addons) AS a
WHERE taskstatus = 1
AND year = 2014
AND month = 04
在这里寻求帮助之前我有什么尝试?
首先,我测试了看看使用taskstatus进行计数是否像这样工作:
SELECT Count(*)
FROM (SELECT title,
taskstatus,
Extract(year FROM finished) AS year,
Extract(month FROM finished) AS month
FROM todo_languages
UNION ALL
SELECT title,
taskstatus,
Extract(year FROM finished) AS year,
Extract(month FROM finished) AS month
FROM todo_main
UNION ALL
SELECT title,
taskstatus,
Extract(year FROM finished) AS year,
Extract(month FROM finished) AS month
FROM todo_addons) AS a
WHERE taskstatus = 1
以上工作正常,并返回当前计数1.接下来,我测试以确保按月和按年计算得分:
SELECT Count(*)
FROM (SELECT title,
taskstatus,
Extract(year FROM finished) AS year,
Extract(month FROM finished) AS month
FROM todo_languages
UNION ALL
SELECT title,
taskstatus,
Extract(year FROM finished) AS year,
Extract(month FROM finished) AS month
FROM todo_main
UNION ALL
SELECT title,
taskstatus,
Extract(year FROM finished) AS year,
Extract(month FROM finished) AS month
FROM todo_addons) AS a
WHERE year = 2014
AND month = 04
这也有效并返回行数1,但第一个查询返回的行数为0,这引出了我的观点。我有没有看到我做错了什么?提前谢谢。
答案 0 :(得分:0)
WHERE taskstatus = 1
,也可以记录WHERE year = 2014 AND month = 04
但不记录WHERE taskstatus = 1 AND year = 2014 AND month = 04
。
想象:
taskstatus year month
1 2000 05
2 2004 04