我有以下查询显示两个表之间匹配的记录:
select DISTINCT ug.name, ug.payroll_no, esr.assignment from user_group AS ug
inner join esrtraining AS esr on ug.payroll_no = SUBSTRING(esr.assignment, 2, 8)
我怎么能这样做呢?例如显示“User_Group”中没有匹配的名称?感谢。
答案 0 :(得分:4)
您可以使用NOT EXISTS
以相同条件执行此操作:
select DISTINCT ug.name, ug.payroll_no
from user_group AS ug
Where Not Exists
(
Select *
From esrtraining AS esr
Where ug.payroll_no = SUBSTRING(esr.assignment, 2, 8)
)
答案 1 :(得分:4)
使用OUTER连接而不是INNER,然后在第二个表中不可为空的列的WHERE子句中查找NULL:
select DISTINCT ug.name, ug.payroll_no, esr.assignment
from user_group AS ug
left join esrtraining AS esr on ug.payroll_no = SUBSTRING(esr.assignment, 2, 8)
where esr.assignment is null
这称为排除加入。