2014-01-15 83 views
0

我真的試圖自己做這件事,但似乎有像我這樣的新手過於複雜的查詢。MySQL多行,分組多個值

上dgpl_se_bets1x2表I具有值:

id, id_mat, prev, note, datetime 

是什麼的這些重​​要的:

id_mat =匹配ID

先前=賭注放置在由用戶,的值是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

試試這個試試

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

這正是我在找的東西。我覺得這很難。非常感謝帕特里克:) – Yuha