Sql是否选择不存在的记录



我正试图将以下3个查询修改为一个具有最终输出的查询。

第一次查询返回ID和位置列表

Select id,place from Global where status='Y';

查询示例输出:-

1( 123,德里

2( 345,查谟

3( 456,哈里亚纳邦

然后我想把这个输出发送到

Licence和DrvingLicence表进行筛选,如果这两个表中的任何一个表中显示的id都会将该id从最终输出中排除。

许可证id=id和驾驶许可证号=id

Select licenceId from Licence table where state='MO' and licenceid=:id
Select drivingLicenceNo from DrvingLicence table where DOB='12/12/1978' and drivingLicenceNo=:id

位置仅存在于"全局">表中,该表应存在于最终输出中。

最终输出:

1( 123,德里

假设id=345存在于Licence表中,id=456存在于DrvingLicence表中

你能试试这个吗,它对我来说很好。最终输出是123,德里

select * from global g
where not exists (select 1 from Licence l where l.licenceId=g.id and l.state='MO')
and not exists (select 1 from DrvingLicence dl where dl.drivingLicenceNo=g.id and dob=to_date('12121978','ddmmyyyy'))

尝试这个

select 
id,
place 
from Global g
where status='Y'
and NOT EXISTS (
select
licenceId
from Licence l
on g.id = l.licenceId
)
OR NOT EXISTS (
select
drivingLicenceNo
from DrvingLicence dl
on g.id = dl.drivingLicenceNo
);

也许可以使用一些JOINS

Select g.id,g.place 
from Global g 
INNER JOIN Licence li ON g.id != li.id 
INNER JOIN DrvingLicence dl ON g.id != dl.id 
WHERE g.status='Y' 
AND li.state='MO' 
AND dl.DOB='12/12/1978';

试试这个吧!

SELECT * FROM GLOBAL g
WHERE g.id NOT IN (Select licenceId from Licence table where state='MO',Select drivingLicenceNo from DrvingLicence table where DOB='12/12/1978')

希望它能起作用。。

最新更新