MySQL order by two values

泄露秘密 提交于 2019-12-01 18:37:21

问题


In short, I have a game of sorts, and the score board for it shows the score of the player at the end + how long they took to achieve it. I need to order my MySQL results so that the highest score in the fastest time is first and the lowest score with the slowest time is last, and everything in between is ranked in the same way. But I have no idea how I'd do that, here's my sql as it stands:

$sql1 = "SELECT * FROM boobmatch_highscores ORDER BY t_score DESC, time ASC";

Any help would be greatly appreciated!

The data in the table:

Score       Time
23        00:04:32
23        00:04:31
25        00:02:32
16        00:06:32
35        00:04:32
2         00:01:17
-13       00:19:32
-3        00:07:27

回答1:


You query is supposed to work perfectly. If you have any issues just enclose the field names in quotes like so:

$sql1 = "SELECT * FROM boobmatch_highscores ORDER BY `t_score` DESC, `time` ASC";

Guessing it's MySQL, else the quotes won't be necessary




回答2:


You should be use below code.. Its working.

$query = "SELECT field1, field2 from tablename ORDER BY field1 DESC, field2 DESC";



回答3:


you should use an integer column for time instead of a varchar,
that's mean convert 00:19:32 to just 1172 seconds,
then is pretty easy for sort




回答4:


I think this is the best solution for arrange order by multiple value.

 SELECT * FROM hotel   
   WHERE confId=13   
   GROUP BY hotelName   
   ORDER BY CASE
   WHEN rating = '5 Star' THEN 1 WHEN rating = '4 Star' THEN 2   
   WHEN rating = '3 Star' THEN 3 WHEN rating = 'BUDGET & EQUIVALENT HOTELS' THEN 4 
 END



回答5:


You've to use this query:

select * from boobmatch_highscores order by time ASC, t_score DESC

Now time is displayed ascending order and score is displayed descending order.




回答6:


just try

$sql1 = "SELECT * FROM boobmatch_highscores ORDER BY t_score * time DESC";


来源:https://stackoverflow.com/questions/10834658/mysql-order-by-two-values

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!