开发者

sort mysql data from separate columns

开发者 https://www.devze.com 2023-03-30 16:44 出处:网络
I have a database that keeps track of game stats. Let\'s say each game consists of three rounds played to 10 points. So part of my table looks like this:

I have a database that keeps track of game stats. Let's say each game consists of three rounds played to 10 points. So part of my table looks like this:

ga开发者_如何学Pythonme | round1home | round1away | round2home | round2away | round3home | round3away 
-----------------------------------------------------------------------------------
  1  |     10     |      7     |     8      |     10     |    10      |     5
  2  |     10     |      4     |     10     |     6      |    6       |     10
  3  |     3      |      10    |     9      |     10     |    7       |     10 
  4  |     10     |      5     |     6      |     10     |    10      |     3
  5  |     8      |      10    |     10     |     7      |    7       |     10
  6  |     10     |      8     |     4      |     10     |    10      |     9

What I need to pull out is a list of games with the lowest 5 scoring away team rounds. In other words using the above table I would want the results to return these rows:

game 4: 3 points
game 2: 4 points
game 1: 5 points
game 4: 5 points
game 2: 6 points

My problem is the fact that I possibly could have the same game show up more than once in the list as game 2 does in the above example. I was able to write a query that worked except for adding in the duplicate games.

I assume I need to do a self join maybe then sort the rows? I'm having trouble wrapping my head around this one! Thanks so much for any help!


You can do something like

SELECT * FROM
(
SELECT game_id, round1away AS away_score
ORDER BY round1away  
LIMIT 5
UNION 
SELECT game_id,  round2away AS away_score
ORDER BY round2away  
LIMIT 5
)a
ORDER BY away_score
LIMIT 5
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号