SQL WHERE.. IN clause multiple columns

前端 未结 13 1185
死守一世寂寞
死守一世寂寞 2020-11-29 15:56

I need to implement the following query in SQL Server:

select *
from table1
WHERE  (CM_PLAN_ID,Individual_ID)
IN
(
 Select CM_PLAN_ID, Individual_ID
 From CR         


        
相关标签:
13条回答
  • 2020-11-29 16:30

    A simple EXISTS clause is cleanest

    select *
    from table1 t1
    WHERE
    EXISTS
    (
     Select * --or 1. No difference...
     From CRM_VCM_CURRENT_LEAD_STATUS Ex
     Where Lead_Key = :_Lead_Key
    -- correlation here...
    AND
    t1.CM_PLAN_ID = Ex.CM_PLAN_ID AND t1.CM_PLAN_ID =  Ex.Individual_ID
    )
    

    If you have multiple rows in the correlation then a JOIN gives multiple rows in the output, so you'd need distinct. Which usually makes the EXISTS more efficient.

    Note SELECT * with a JOIN would also include columns from the row limiting tables

    0 讨论(0)
提交回复
热议问题