我有几个类似的查询,仅在条件子句中有所不同。我正在尝试将所有查询合并为一个。
第一个查询如下所示:
第一个查询:
SELECT pages.title,
YEAR(page_revisions.date) AS year,
MONTH(page_revisions.date) AS MONTH,
COUNT(revid) AS Cadmin_edits
FROM page_revisions
INNER JOIN pages ON page_revisions.title=pages.title
WHERE userid!=0 AND admin=1 AND pages.ns=0
GROUP BY year, MONTH
ORDER BY page_revisions.title, year, MONTH;
+--------------+------+-------+--------------+
| title | year | MONTH | Cadmin_edits |
+--------------+------+-------+--------------+
| Bangalore | 2002 | 11 | 4 |
| Bangalore | 2003 | 2 | 2 |
| Bangalore | 2003 | 5 | 1 |
| Bangalore | 2003 | 6 | 6 |
| Bangalore | 2003 | 8 | 4 |
| Bangalore | 2003 | 9 | 3 |
| Bangalore | 2003 | 10 | 2 |
| Bart Simpson | 2002 | 9 | 4 |
| Bart Simpson | 2007 | 3 | 33 |
| Bart Simpson | 2007 | 7 | 15 |
| Bart Simpson | 2008 | 11 | 19 |
| Batman | 2001 | 12 | 5 |
第二个查询非常相似,唯一的区别在于条件 wikip_member=1 而不是 admin。
第二个查询:
SELECT pages.title,
YEAR(page_revisions.date) AS year,
MONTH(page_revisions.date) AS MONTH,
COUNT(revid) AS Wpart_edits
FROM page_revisions
INNER JOIN pages ON page_revisions.title=pages.title
WHERE userid!=0 AND wikip_member=1 AND pages.ns=0
GROUP BY year, MONTH
ORDER BY page_revisions.title, year, MONTH;
+------------------------------------------+------+-------+-------------+
| title | year | MONTH | Wpart_edits |
+------------------------------------------+------+-------+-------------+
| 1906 Florida Keys hurricane | 2011 | 10 | 266 |
| 1906 Florida Keys hurricane | 2011 | 11 | 108 |
| 1906 Florida Keys hurricane | 2012 | 2 | 66 |
| 1969 race riots of Singapore | 2007 | 6 | 124 |
| 1969 race riots of Singapore | 2008 | 2 | 143 |
| 1969 race riots of Singapore | 2009 | 1 | 151 |
| 1980 Winter Olympics | 2003 | 8 | 3 |
| 1980 Winter Olympics | 2003 | 10 | 7 |
| 1980 Winter Olympics | 2004 | 3 | 16 |
| Babe Ruth | 2004 | 5 | 22 |
| Babe Ruth | 2004 | 7 | 21 |
| Bangalore | 2002 | 11 | 6 |
| Bangalore | 2003 | 2 | 2 |
| Bangalore | 2004 | 2 | 9 |
基本上我需要的输出看起来像这样:
预期输出:
+------------------------------------------+------+-------+--------+-------+
| title | year | MONTH | Cadmin | Wpart |
+------------------------------------------+------+-------+--------+-------+
| 1906 Florida Keys hurricane | 2011 | 10 | 0 | 266 |
| 1906 Florida Keys hurricane | 2011 | 11 | 0 | 108 |
| 1906 Florida Keys hurricane | 2012 | 2 | 0 | 66 |
| 1969 race riots of Singapore | 2007 | 6 | 0 | 124 |
| 1969 race riots of Singapore | 2008 | 2 | 0 | 143 |
| 1969 race riots of Singapore | 2009 | 1 | 0 | 151 |
| 1980 Winter Olympics | 2003 | 8 | 0 | 3 |
| 1980 Winter Olympics | 2003 | 10 | 0 | 7 |
| 1980 Winter Olympics | 2004 | 3 | 0 | 16 |
| Babe Ruth | 2004 | 4 | 0 | 7 |
| Babe Ruth | 2004 | 8 | 0 | 21 |
| Bangalore | 2002 | 11 | 4 | 6 |
| Bangalore | 2003 | 2 | 2 | 2 |
| Bangalore | 2003 | 5 | 1 | 0 |
| Bangalore | 2003 | 6 | 6 | 0 |
| Bangalore | 2003 | 8 | 4 | 0 |
| Bangalore | 2003 | 9 | 3 | 0 |
| Bangalore | 2003 | 10 | 2 | 0 |
| Bangalore | 2004 | 2 | 0 | 9 |
这将是查询的组合。基本上获取每个月的修订的 COUNT 并在没有找到与该月的条件匹配的修订时显示 0。我尝试了UNION、SUBSELECT等几种方法,但都没有成功。UNION 似乎没有按预期工作,SUBSELECT 只运行了 128 行需要 5 个小时。
联合尝试:
SELECT pages.title,
YEAR(page_revisions.date) AS year,
MONTH(page_revisions.date) AS MONTH,
COUNT(revid) AS Cadmin,
null AS Wpart
FROM page_revisions
INNER JOIN pages ON page_revisions.title=pages.title
WHERE userid!=0 AND admin=1 AND pages.ns=0
UNION
SELECT pages.title,
mid( page_revisions.date, 1, 4) AS year,
mid(page_revisions.date, 6, 2) AS MONTH,
null as Cadmin,
COUNT(revid) AS Wpart_edits
FROM page_revisions
INNER JOIN pages ON page_revisions.title=pages.title
WHERE userid!=0 AND wikip_member=1 AND pages.ns=0
GROUP BY year, MONTH
ORDER BY title, year, MONTH;
子选择尝试:
SELECT pages.title,
YEAR(page_revisions.date) AS year,
MONTH(page_revisions.date) AS MONTH,
(SELECT COUNT(revid) AS Cadmin_edits FROM page_revisions WHERE YEAR(page_revisions.date)=year and MONTH(page_revisions.date)= month and userid!=0 AND admin=1)
FROM page_revisions
INNER JOIN pages ON page_revisions.title=pages.title
WHERE pages.ns=0
GROUP BY year, MONTH
ORDER BY pages.title, year, MONTH;
任何为我指明正确方向的帮助将不胜感激。这是我的最后一次演讲,该演讲将于周五到期,这是我需要将所有内容包装在一起的最后一个查询。