考虑一个包含2列的Employee
表
INT数据类型
的IDNAME是VARCHAR数据类型。
考虑第二个Employee_PHONE
表,其中包含2列
INT数据类型
的IDPHONE,其为VARCHAR数据类型。 两者都设置为NOT NULL。
EMPLOYEE_PHONE的ID是引用EMPLOYEE表主键的外键
员工可能没有任何电话或可能有多部电话。 什么是SQL查询来显示没有电话的所有员工的姓名?
答案 0 :(得分:1)
这可以直接从“英语转换为SQL”
select e.*
from employee e ---<<< get me all employees
where not exists ---<<< that do not have a phone
(select *
from employee_phone ep
where ep.employee_id = e.id
and ep.phone <> ''); -- in case you allow empty strings in that column
答案 1 :(得分:0)
这是两个选择查询。
SELECT * FROM Employee a WHERE a.ID NOT IN (SELECT b.ID FROM Employee_PHONE)
SELECT * FROM Employee a
LEFT JOIN Employee_PHONE b ON a.ID = b.ID WHERE ISNULL(b.PHONE,'') = ''
答案 2 :(得分:0)
select E.Name
from Employee E left Join Employee_PHONE EP
on E.ID = EP.ID
where EP.EMPLOYEE_PHONE is null or EP.EMPLOYEE_PHONE = ''
答案 3 :(得分:0)
试试这个:
select E.Name
from Employee E left Join Employee_PHONE EP
on E.ID = EP.ID
where EP.EMPLOYEE_PHONE is null