简体   繁体   English

MySQL多行,分组多个值

[英]MySQL multiple rows, grouping multiple values

I really tried to do this by myself but seems that there is too complex query for a newbie like me. 我确实尝试自己完成此操作,但是对于像我这样的新手来说,查询太复杂了。

on dgpl_se_bets1x2 table i have values for: 在dgpl_se_bets1x2表上,我具有以下值:

id, id_mat, prev, note, datetime

what is important of these: 这些重要的是什么:

id_mat = match id id_mat =匹配ID

prev = bet placed by user, values are 1, x or 2. prev =用户下注,值为1,x或2。

I want to print statistics about made bets, here is few lines for example: 我想打印有关下注的统计信息,例如以下几行:

+--+------+----+----+-------------------+
|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|
+--+------+----+----+-------------------+

So there are matches 5,6,7 and 8. And from that data I want to print out something like this: 因此,有匹配项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|
+------------+-+-+-+

Or even better, with percentages: 甚至更好,以百分比表示:

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

Give this a try 试试这个

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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM