简体   繁体   中英

MySQL - Order By column values, group according other column

Name    1-3    4-6   7-9
------------------------
Dan       0      3     4
Jane      1      6     0
Mike      3      3     3
Dan       2      2     0
Jane      3      4     2
Dan       1      3     0

Here's order that needed to be done.

Name    1-3    4-6   7-9
------------------------
Dan       0      3     4
Dan       1      3     0
Dan       2      2     0
Mike      3      3     3
Jane      3      4     2
Jane      2      2     0

In short, the order by start with the highest number in field 7-9 have then group by the name, then order by again in each group name (in this ex. is Dan & Jane ) according field 4-6 and 1-3

Try this query:

SELECT Name, `1-3`, `4-6`, `7-9`
FROM yourTable
ORDER BY `7-9` DESC, Name, `4-6` DESC, `1-3` DESC

I think there is typo in your desired result, and try this:

select t1.*
from yourtable t1
join (
  select Name, max(`7-9`) orderby
  from yourtable
  group by Name
  order by max(`7-9`)
) t2 on t1.Name = t2.Name
order by orderby desc, `4-6` desc, `1-3`

Demo Here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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