问题
In relation to: MySQL ORDER BY Customized
I have another question.
We have an id_competitor
with various scores.
id_competitor score
1 WIN
2 50+
3 90+
4 90+
1 50
2 WIN
3 40
4 40+
I would like to use order by
but in the following order:
id_competitor
2
1
4
3
I dont know how i should do it, with SELECT DISTINCT with ORDER BY or GROUP BY
回答1:
based on other answer, I would do something like
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
回答2:
To expand on Ed's excellent response ( 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
来源:https://stackoverflow.com/questions/17050225/mysql-order-by-or-group-by