MySQL ORDER BY或GROUP BY

时间:2013-06-11 17:30:45

标签: mysql group-by sql-order-by

关于:MySQL ORDER BY Customized 我有另一个问题。 我们有id_competitor个各种分数。

id_competitor   score
1               WIN
2               50+
3               90+
4               90+
1               50
2               WIN
3               40
4               40+

我想使用order by但按以下顺序:

id_competitor
2
1
4
3

我不知道应该怎样做,SELECT DISTINCT with ORDER BY or GROUP BY

2 个答案:

答案 0 :(得分:1)

基于其他答案,我会做类似

的事情
SELECT s.id_competitor 
FROM (
SELECT 
  id_competitor, 
  SUM(CASE
       WHEN score = 'WIN' THEN 100000
       WHEN score = 'LOSER' THEN -100000
       WHEN score LIKE '%+' THEN score * 100 + 99
       ELSE score * 100
       END) as score
FROM myTable
GROUP BY id_competitor) as s
ORDER BY s.score DESC

SqlFiddle

答案 1 :(得分:0)

扩展Ed的出色回应(https://stackoverflow.com/users/2091410/ed-gibbs

select outside.id_competitor, sum(outside.numericscore) as sumscore
from (
select inside.id_competitor, CASE
    WHEN inside.score = 'WINNER' THEN 100000
    WHEN inside.score = 'LOSER' THEN -100000
    WHEN inside.score LIKE '%+' THEN inside.score * 100 + 99
    ELSE inside.score * 100
    END AS "numericscore" 
FROM mytable inside
) as outside
group by outside.id_competitor
order by sumscore desc