简体   繁体   中英

Convert sub-query with “NOT IN” operator to join with multiple tables

I need to convert the following sub-query to JOIN. Here I already have JOIN operator in the inner query. Please help.

SELECT * 
FROM   Consultants 
WHERE  Consultants.ConsIntID 
NOT IN  (SELECT Links.ToID 
         FROM   Links JOIN Reminders 
         ON Links.FromID = Reminders.RemIntID 
            AND ApptSubType = 'Placed' 
            AND ToID LIKE 'CS%')

Alright so you probably shouldn't change this to a join I would use NOT EXISTS the reasons for doing so are stated here

I've also replace your ancient join syntax and added aliases to clear this up. The method shown below has been the accepted standard for about 22 years now and is the preferred way to write queries.

SELECT C.* 
FROM   Consultants  as C -- aliases are very useful for clarity
WHERE  
NOT EXISTS (
        SELECT 1
         FROM   Links as L 
         INNER JOIN Reminders  as R --New join syntax
            ON  L.FromID = R.RemIntID 
         WHERE C.ConsIntID = L.ToID 
         AND ApptSubType = 'Placed' 
         AND ToID LIKE 'CS%'
            ) 
SELECT * 
FROM   Consultants 
WHERE  Consultants.ConsIntID 
NOT IN  (SELECT Links.ToID 
         FROM   Links 
         JOIN   Reminders  ON(Links.FromID = Reminders.RemIntID)
         WHERE  ApptSubType = 'Placed' 
                AND ToID LIKE 'CS%') 

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