我的桌子
+------+-------+--------+
| NAME | MARKS | POINTS |
+------+-------+--------+
| S1 | 53 | (null) |
| S2 | 55 | (null) |
| S3 | 56 | (null) |
| S4 | 55 | (null) |
| S5 | 52 | (null) |
| S6 | 51 | (null) |
| S7 | 53 | (null) |
+------+-------+--------+
Run Code Online (Sandbox Code Playgroud)
参考:http://www.sqlfiddle.com/#!2/5d046/1
我想在最高分上加3,2,1分.这里S3到3点,S2,S4到2点,S1,S7到1点.
最终输出看起来,
+------+-------+--------+
| NAME | MARKS | POINTS |
+------+-------+--------+
| S1 | 53 | 1 |
| S2 | 55 | 2 |
| S3 | 56 | 3 |
| S4 | 55 | 2 |
| S5 | 52 | 0 |
| S6 | 51 | 0 |
| S7 | 53 | 1 |
+------+-------+--------+
Run Code Online (Sandbox Code Playgroud)
Plz的帮助
您可以通过变量(请参阅其他答案中的示例)或通过case:
select
myTable.*,
case
when max1.marks is not null then 3
when max2.marks is not null then 2
when max3.marks is not null then 1
else 0
end as score
from
myTable
LEFT JOIN
(select marks from myTable order by marks desc limit 1) AS max1
ON myTable.marks=max1.marks
LEFT JOIN
(select marks from myTable order by marks desc limit 2,1) AS max2
ON myTable.marks=max2.marks
LEFT JOIN
(select marks from myTable order by marks desc limit 3,1) AS max3
ON myTable.marks=max3.marks;
Run Code Online (Sandbox Code Playgroud)
该演示可以在这里找到。
| 归档时间: |
|
| 查看次数: |
82 次 |
| 最近记录: |