I had an original question relating to how to find the greatest value and it was helpfully solved.使用以下查询即可完成:
select t.*,(case greatest(score1,score2,score3)
when score1 then 'score1'
when score2 then 'score2'
when score3 then 'score3'
end) as high_score_name,greatest(score1,score3) as high_score
from t;
这就像一个吊饰,并创建所需的输出,如下所示:
+------+----------+----------+----------+--------------+
| ID | score1 | score2 | score3 | high_score |
+------+----------+----------+----------+--------------+
| 123 | 14 | 561 | 580 | score3 |
| 456 | 626 | 771 | 843 | score3 |
| 789 | 844 | 998 | 904 | score2 |
| 111 | 922 | 677 | 301 | score1 |
| 222 | 665 | 578 | 678 | score3 |
| 333 | 416 | 631 | 320 | score2 |
+------+----------+----------+----------+--------------+
但是现在我试图找到第二个greates值,以便生成的输出看起来像这样:
+------+----------+----------+----------+--------------+--------------+
| ID | score1 | score2 | score3 | high_score | second_score |
+------+----------+----------+----------+--------------+--------------+
| 123 | 14 | 561 | 580 | score3 | score2 |
| 456 | 626 | 771 | 843 | score3 | score2 |
| 789 | 844 | 998 | 904 | score2 | score3 |
| 111 | 922 | 677 | 301 | score1 | score2 |
| 222 | 665 | 578 | 678 | score3 | score1 |
| 333 | 416 | 631 | 320 | score2 | score1 |
+------+----------+----------+----------+--------------+--------------+
回答问题的原始人建议提出一个新问题。我可能应该考虑平局,但是我们的数字已经足够小数位打破任何数字了。