选择MySQL查询选择

时间:2016-03-01 12:26:35

标签: mysql

Country Value Type
AU        152 IN
AU         56 OUT
IN         85 IN
CA         74 OUT
CA         98 IN
IN         89 IN
US        124 IN
UK        856 OUT
IN         35 OUT
AU         26 IN
PS         24 OUT
CN        869 OUT

我需要一个对国家/地区进行分组的查询,并分别打印IN和OUT字段的总和 提前谢谢你 enter image description here

1 个答案:

答案 0 :(得分:0)

它将为您提供所有个人国家的统计数据

SELECT country, 
SUM(case when type = 'IN' then 1 END) AS IN,
SUM(case when type = 'OUT' then 1 END) AS OUT 
FROM table_name 
GROUP BY country

它将为您提供所有个人所有国家/地区的价值

SELECT country, 
SUM(case when type = 'IN' then value END) AS IN,
SUM(case when type = 'OUT' then value END) AS OUT 
FROM table_name 
GROUP BY country