使用 "not in" 在 MySQL 中设置为空

Getting Empty Set in MySQL on using "not in"

我是 MySQL 的初学者。因此,可能会出现一些错误。我有一个用于学习目的的员工部门数据库。我为有主管的员工存储了 supervisorENO。如果员工没有主管,则其主管ENO 为空。

我必须检索不是主管的员工姓名。我 运行 以下命令并设置为空。我没弄明白。

这是我的 table:

MariaDB [EMP_DEPT]> select * from EMPLOYEE;
+-----+---------------+---------------------------+---------------+------------+------+------------+----------+
| Eno | Ename         | Job_type                  | SupervisorENO | Hire_date  | Dno  | Commission | Salary   |
+-----+---------------+---------------------------+---------------+------------+------+------------+----------+
| 111 | Aman Singh    | HR Manager                | NULL          | 2000-01-23 |   50 |       NULL |  5000.00 |
| 112 | Ankesh Kumar  | HR Assistant              | 111           | 2005-10-30 |   50 |       NULL |  4000.00 |
| 113 | Gaurav Singh  | Account Manager           | NULL          | 2002-07-09 |   60 |     100.00 |  6000.00 |
| 114 | Sanjeet Kumar | Accounting Clerk          | 113           | 2015-04-18 |   60 |       NULL |  4500.00 |
| 115 | Rajnish Yadav | Production Manager        | NULL          | 1980-12-04 |   10 |     150.00 |  5500.00 |
| 116 | Sumit Sharan  | Production Incharge       | 115           | 1995-02-24 |   10 |       NULL |  4500.00 |
| 117 | Amartya Sinha | R&D Scientist             | NULL          | 2010-03-15 |   20 |       NULL | 10000.00 |
| 118 | Shahnwaz Khan | R&D Associate Engineer    | 117           | 2016-05-23 |   20 |       NULL |  4000.00 |
| 119 | Sonu Giri     | Purchase Executive        | NULL          | 2013-06-17 |   30 |     140.00 |  7000.00 |
| 120 | Kaushik Kumar | Purchase Specialist       | 119           | 2018-08-13 |   30 |    4500.00 |  4000.00 |
| 121 | Vishal Yadav  | Chief Marketing Officer   | NULL          | 1995-11-19 |   40 |     250.00 | 10000.00 |
| 122 | Satyam Jha    | Digital Marketing Manager | 121           | 2004-09-29 |   40 |       NULL |  4500.00 |
+-----+---------------+---------------------------+---------------+------------+------+------------+----------+
12 rows in set (0.001 sec)

MariaDB [EMP_DEPT]> select Ename from EMPLOYEE where Eno not in (select distinct SupervisorENO from EMPLOYEE);
Empty set (0.001 sec)

我期待 Eno 112、114、116、118、120、122 的员工姓名,因为他们不是主管。请帮我弄清楚。

select distinct SupervisorENO from EMPLOYEE 也会 return NULL,与 NULL 比较时的任何值实际上都不会 return True 或 False,它会 return 未知,可以这么说。您可以阅读更多相关信息 here for example

因此,为了修复您的查询,您需要排除 NULL:

select Ename 
  from employee 
 where Eno not in (select distinct SupervisorENO 
                     from employee 
                    where supervisoreno is not null);

Here's a working demo on dbfiddle

您可以简单查询

select Ename from employee 
where SupervisorEno is not null;

这将 return 那些有主管的人,即。自己不是主管。

select Ename from employee 
where SupervisorEno is null;

请问各位监督return

非严格需要时最好避免sub-queries。