Mysql如何选择和统计多条记录?

时间:2014-08-19 15:19:12

标签: mysql

我有这样的表:

enter image description here

我想得到这样的结果:

a win=2 lose=1 total_bet_result=3
b win=1 lose=1 total_bet_result=2
c win=1 lose=2 total_bet_result=3

2 个答案:

答案 0 :(得分:1)

SELECT user, 
        SUM(bet_result = 'win') AS win, 
        SUM(bet_result = 'lose') AS lose
        COUNT(*) AS total_bet_result
FROM YourTable
GROUP BY user

答案 1 :(得分:0)

试试这个,

SELECT user,
    SUM(CASE WHEN bet_result = 'win' THEN 1 END) AS 'win',
    SUM(CASE WHEN bet_result = 'lose' THEN 1 END) AS 'lose',
    count(bet_result) as total_bet
FROM yourtable
GROUP BY user