2

我想加入两个或多个具有 group by 子句的 select 语句,但它不起作用。

SELECT COUNT(`rank`) AS rank1,sitename
FROM `larrdb_ranking` 
INNER JOIN larrdb_site_list USING (sid)
WHERE rank=1
GROUP BY sitename
UNION 
SELECT COUNT(`rank`) AS rank2 ,sitename
FROM `larrdb_ranking` 
INNER JOIN larrdb_site_list USING (sid)
WHERE rank=2
GROUP BY sitename

我得到的只是第一个语句有效,但第二个语句没有..我没有选择 rank2

更新:

Union All 也不起作用:

    SELECT COUNT(`rank`) AS rank1,sitename
FROM `larrydb_ranking` 
INNER JOIN larrydb_site_list USING (sid)
WHERE rank=1
GROUP BY sitename
UNION ALL
SELECT COUNT(`rank`) AS rank2 ,sitename
FROM `larrydb_ranking` 
INNER JOIN larrydb_site_list USING (sid)
WHERE rank=2
GROUP BY sitename

column..rank2 未显示

4

1 回答 1

1

试试这个:

SELECT SUM(IF(rank=1, 1, 0)) AS rank1, SUM(IF(rank=2, 1, 0)) AS rank2,sitename
FROM `larrydb_ranking` 
INNER JOIN larrydb_site_list USING (sid)
GROUP BY sitename
于 2012-11-26T07:03:47.477 回答