Mysql计数语句和组

时间:2014-05-11 23:32:32

标签: mysql join count union

我有一张桌子:

id  user type   
11   4    1         
12   4    1         
13   4    1         
14   5    2          

然后我需要计算并订购用户并输入如下:

user type1 type2
4      3    0
5      0    1

我做的sql语句只是结果:

user type1 type2
4      3    1
5      3    1

有人能帮助我吗?

2 个答案:

答案 0 :(得分:0)

这应该有效

select user, sum(if(type=1,1,0)) as type1, sum(if(type=2,1,0)) as type2 from table_name group by user

答案 1 :(得分:-1)

尝试类似这样的事情:

SELECT id, user, type, COUNT(type) as count 
FROM table_name
GROUP BY user

看到一些代码会有所帮助。