我有3个查询完美无缺
SELECT snapdate, COUNT( DISTINCT uid) AS t1
FROM table where tid =1
GROUP BY snapdate
ORDER BY snapdate DESC LIMIT 7
查询#1:
snapdate t1
---------- --
2013-07-02 10
2013-07-01 20
2013-06-30 60
2013-06-29 80
2013-06-28 3
2013-06-27 22
2013-06-26 93
查询#2:
SELECT snapdate, COUNT( DISTINCT uid) AS t2
FROM table where tid =2
GROUP BY snapdate
ORDER BY snapdate DESC LIMIT 7
snapdate t2
---------- --
2013-07-02 35
2013-07-01 52
2013-06-30 69
2013-06-29 75
2013-06-28 91
2013-06-27 97
2013-06-26 93
查询#3:
SELECT snapdate, COUNT( DISTINCT uid) AS t3
FROM table where tid =3
GROUP BY snapdate
ORDER BY snapdate DESC LIMIT 7
snapdate t3
---------- --
2013-07-02 22
2013-07-01 22
2013-06-30 26
2013-06-29 27
2013-06-28 29
2013-06-27 29
2013-06-26 29
但我不知道如何一起得到3个结果
snapdate t1 t2 t3
---------- -- -- --
2013-07-02 10 35 22
2013-07-01 20 52 22
2013-06-30 60 69 26
2013-06-29 80 75 27
2013-06-28 3 91 29
2013-06-27 22 97 29
2013-06-26 93 93 29
答案 0 :(得分:6)
您可以在聚合函数中使用CASE表达式:
SELECT snapdate,
COUNT(DISTINCT case when tid =1 then uid end) AS t1,
COUNT(DISTINCT case when tid =2 then uid end) AS t2,
COUNT(DISTINCT case when tid =3 then uid end) AS t3
FROM table
GROUP BY snapdate
ORDER BY snapdate DESC
LIMIT 7