简体   繁体   English

在 SQL 中的 ORDER BY 子句下使用 CASE

[英]Using CASE under ORDER BY clause in SQL

Write a SQL query to find all the details of 1970 winners by the ordered to subject and winner name;编写 SQL 查询,按主题和获奖者姓名查找 1970 年获奖者的所有详细信息; but the list contain the subject Economics and Chemistry at last.但该列表最后包含主题经济学和化学。

SELECT *
FROM nobel_win
WHERE year=1970 
ORDER BY
 CASE
    WHEN subject IN ('Economics','Chemistry') THEN 1 //what does 1 mean?
    ELSE 0 //what does 0 mean
 END ASC,
 subject,
 winner;

The case expression returns 1 is the subject is Economics or Chemistry and 0 otherwise. case主题是经济学或化学,则case表达式返回 1,否则返回 0。 This means that rows with those subjects will get sorted after all the other rows.这意味着具有这些主题的行将所有其他行之后排序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM