MySQL多行,分组多个值

时间:2014-01-15 20:54:07

标签: mysql sql

我真的试图自己这样做,但似乎对像我这样的新手来说有太复杂的查询。

在dgpl_se_bets1x2表上我有以下值:

id, id_mat, prev, note, datetime

重要的是:

id_mat =匹配ID

prev =用户下注的赌注,值为1,x或2。

我想打印关于下注的统计数据,例如这里有几行:

+--+------+----+----+-------------------+
|id|id_mat|prev|note|datetime           |
+--+------+----+----+-------------------+
|6 |442   |2   |1   |2014-01-03 11:04:08|
+--+------+----+----+-------------------+
|5 |442   |1   |1   |2014-01-03 11:04:08|
+--+------+----+----+-------------------+
|5 |449   |1   |1   |2014-01-02 16:40:28|
+--+------+----+----+-------------------+
|6 |449   |1   |0   |2014-01-02 16:40:28|
+--+------+----+----+-------------------+
|7 |442   |2   |0   |2014-01-03 11:04:08|
+--+------+----+----+-------------------+
|8 |442   |2   |1   |2014-01-03 11:04:08|
+--+------+----+----+-------------------+
|7 |636   |2   |0   |2014-01-03 15:46:34|
+--+------+----+----+-------------------+

所以有比赛5,6,7和8。 从那些数据我想打印出这样的东西:

+------------+-+-+-+
|Match Number|1|x|2|
+------------+-+-+-+
|5           |2|0|0|
+------------+-+-+-+
|6           |1|0|1|
+------------+-+-+-+
|7           |0|0|2|
+------------+-+-+-+
|8           |0|0|1|
+------------+-+-+-+

甚至更好,百分比:

+------------+---+-+---+
|Match Number|1  |x|2  |
+------------+---+-+---+
|5           |100|0|0  |
+------------+---+-+---+
|6           |50 |0|50 |
+------------+---+-+---+
|7           |0  |0|100|
+------------+---+-+---+
|8           |0  |0|100|
+------------+---+-+---+

1 个答案:

答案 0 :(得分:1)

试一试

SELECT id_mat,
    (SUM(prev='1')/COUNT(*))*100 AS prev1,
    (SUM(prev='x')/COUNT(*))*100 AS prevX,
    (SUM(prev='2')/COUNT(*))*100 AS prev2
FROM dgpl_se_bets1x2
GROUP BY id_mat;