1. 검색하면 흔히 나오는 사용자 변수를 활용해서 랭킹 구하는 쿼리
+ 0점, null일경우 처리
2. 쿼리
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
<![CDATA[
SELECT no, score, CAST(rank AS CHAR) AS rank FROM (
SELECT no
,score
,CASE
WHEN @prev_value = score THEN @vRank
WHEN @prev_value := score THEN @vRank := @vRank + 1
WHEN @prev_value = '0.00' THEN @vRank := @vRank + 1
WHEN @prev_value = null THEN @vRank := @vRank + 1
END AS rank
FROM test t ,
(SELECT @vRank := 0, @prev_value := NULL) AS r
ORDER BY score DESC
) AS A
]]>
|
cs |
3. 결과
총 4명중
1등 3점
2등 2점
3등 0점
3등 0점
으로 표시된다
'개발' 카테고리의 다른 글
mysql | Query execution was interrupted (0) | 2021.04.05 |
---|---|
mysql workbench | table insert 안될때, insert버튼 없을때 (0) | 2021.04.01 |
jsp | Uncaught RangeError: Maximum call stack size exceeded (0) | 2021.03.09 |
MySQL | Error Parsing DDL | There was an error while parsing the DDL retrieved from the server (0) | 2021.02.02 |
HTML | image map area 클릭시 선 없애기 (0) | 2020.12.13 |