简体   繁体   中英

Sql Select not existing record?

I am trying to modify following 3 queries into one query with Final output.

The First Query Returns List of ids and Places

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

Query example Output:-

1)123,Delhi

2)345,Jammu

3)456,Haryana

Then I want to send this output to

Licence, and DrvingLicence tables to filter,if the id presents in either of this tables exclude the id from final output.

licenceId=id and drivingLicenceNo=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

Place is only present in "Global" table which should be present in Final output.

Final Output:

1)123,Delhi

Assuming id=345 present in Licence table and id=456 present in DrvingLicence table

Can you try this one,it worked right for me. Final output is 123,Delhi

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'))

try this

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
);

Maybe use some 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';

Try this one instead!

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')

Hope it works..

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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