简体   繁体   English

SQL Case 不标记 null 时

[英]SQL Case When not labeling null

I'm trying to do this Leet Code Problem :我正在尝试解决这个Leet Code 问题

Write a SQL query to get the second highest salary from the Employee table.编写 SQL 查询以从 Employee 表中获取第二高的薪水。

+----+--------+
| Id | Salary |
+----+--------+
| 1  | 100    |
| 2  | 200    |
| 3  | 300    |
+----+--------+

For example, given the above Employee table, the query should return 200 as the second highest salary.例如,给定上面的 Employee 表,查询应该返回 200 作为第二高的薪水。 If there is no second highest salary, then the query should return null.如果没有第二高的薪水,则查询应返回 null。

+---------------------+
| SecondHighestSalary |
+---------------------+
| 200                 |
+---------------------+

I'm trying to give this solution:我试图给出这个解决方案:

SELECT 
      CASE WHEN Salary = '' 
         THEN NULL 
         ELSE Salary END SecondHighestSalary 
   FROM 
      Employee 
   ORDER BY 
      SecondHighestSalary 
   LIMIT 1,1;

When there is a second salary, it works fine and returns the output.当有第二个薪水时,它工作正常并返回输出。 However, when there is no second salary and there's only one salary only an empty string is returned.但是,当没有第二个薪水并且只有一个薪水时,只会返回一个空字符串。 I'm trying to return NULL , however, it doesn't return NULL like what I wrote in my query.我正在尝试返回NULL ,但是,它不像我在查询中写的那样返回NULL How can I fix this?我怎样才能解决这个问题?

Your CASE expression is testing the salary in each row of the table, not the one selected by the LIMIT clause.您的CASE表达式正在测试表的每一行中的薪水,而不是LIMIT子句选择的LIMIT Ordering is done after generating the values in the SELECT list, since you can order by those calculated values.排序是在SELECT列表中生成值之后完成的,因为您可以按这些计算值排序。

Since none of the salaries are empty strings, the condition in your CASE will never be true, so it always returns the Salary value.由于没有一个薪水是空字符串,你的CASE的条件永远不会为真,所以它总是返回Salary值。 As a result, your query is equivalent to因此,您的查询相当于

SELECT Salary AS SecondHighestSalary
FROM Employee
ORDER BY SecondHighestSalary
LIMIT 1, 1

Other things:其他事情:

  1. You need to use DESC to get the highest salary at the beginning.一开始需要用DESC才能拿到最高工资。 So even if your method worked, it would find the second lowest salary.所以即使你的方法有效,它也会找到第二低的工资。
  2. Your method doesn't handle the case where multiple employees are tied for the highest salary.您的方法无法处理多名员工获得最高薪水的情况。 LIMIT 1, 1 will return the second row, which will be one of the tied employees. LIMIT 1, 1将返回第二行,这将是绑定的员工之一。

You can solve the second problem using a subquery that removes duplicates:您可以使用删除重复项的子查询来解决第二个问题:

SELECT DISTINCT Salary
FROM Employee

So a final query could be:所以最后的查询可能是:

SELECT IF(COUNT(*) > 0, MAX(Salary), NULL) AS SecondHighestSalary
FROM (
    SELECT DISTINCT Salary
    FROM Employee
    ORDER BY Salary DESC
    LIMIT 1, 1
) AS x

Eg例如

 select max(salary) salary 
   from employee 
 where salary not in 
 (select max(salary) from employee)

You can use this solution, where if there have no second highest salary will return NULL.您可以使用此解决方案,如果没有第二高的薪水将返回 NULL。 Otherwise will return second highest salary否则将返回第二高工资

SELECT 
   IFNULL(MIN(Salary) , 'NULL') as SecondHighestSalary
FROM
    Employee
WHERE salary > (SELECT MIN(salary)
                 FROM Employee)

Or if you want DB default null then just remove IFNULL condition或者,如果您希望 DB 默认为 null,则只需删除 IFNULL 条件

SELECT 
   MIN(Salary) as SecondHighestSalary
FROM
    Employee
WHERE salary > (SELECT MIN(salary)
                 FROM Employee)

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

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