MySql分组查询

今天遇到一个问题,要实现按照条件查询sum(person)这样的结果,但是person还是分为person1和person2的要获得3个值:

1.sum(person)

2.sum(person1)

3.sum(person2)

最后实现的是:2,3,1要在代码中实现了,SQL如下:

SELECT SUM(s.count_add_scenic) AS sumsecnic,
CASE WHEN s.tourist_provice_id='340000' THEN 'shengnei'
WHEN s.tourist_provice_id<>'340000' THEN 'shengwai'
END AS 'touristfrom'
FROM tb_g_temp_scenic_realtime_flow s,
(SELECT DATE_FORMAT(NOW(),'%Y-%m-%d') AS t) t,
(SELECT NOW() AS n) n
WHERE
s.tourist_country_id='I999'
AND s.scenic_province_id='340000'
AND s.data_time>=t.t
AND s.data_time<=n.n
GROUP BY
CASE WHEN s.tourist_provice_id='340000' THEN 'shengnei'
WHEN s.tourist_provice_id<>'340000' THEN 'shengwai'
END

执行结果如下:

 

如果有人知道如何同时得到1,2,3,请告知,谢谢!

你可能感兴趣的:(MySql分组查询)