简体   繁体   中英

SQL--Pairwise condition

Does SQL have any pairwise conditions in the WHERE clause that aren't using IN? My use case is I have two tables: (Date, ID) from table ABC and (LaunchDate, ID) from table XYZ. My condition is that ABC.ID is in XYZ.ID and ABC.Date>=XYZ.LaunchDate (for that corresponding ID). However, if I have (3/5, 1) in ABC and (3/4, 1) in XYZ, a pairwise IN won't work because (3/5, 1) isn't in XYZ?

What I have right now:

select date,ID 
from ABC where (date,ID) in (select LaunchDate, ID 
                             from XYZ 
                             where date>=LaunchDate)

You seem to want exists :

where exists (select 1
              from xyz
              where xyz.id = abc.id and abc.date >= xyz.lunchdate
             )

In general, I recommend exists over in anyways with subqueries, because exists is usually simpler to optimize (although some databases do quite well optimizing in ).

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