2013-06-11 242 views
-1

關於:MySQL ORDER BY Customized 我還有一個問題。 我們有一個id_competitor與各種分數。MySQL ORDER BY或GROUP BY

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

回答

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

+0

DESC,當然。 –

+0

謝謝,快速回答! – user2474683

0

爲了擴展版的出色響應(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