繁体   English   中英

SQL--成对条件

[英]SQL--Pairwise condition

SQL 在 WHERE 子句中是否有任何不使用 IN 的成对条件? 我的用例是我有两个表:表 ABC 中的 (Date, ID) 和表 XYZ 中的 (LaunchDate, ID)。 我的条件是 ABC.ID 在 XYZ.ID 和 ABC.Date>=XYZ.LaunchDate (对应的 ID)。 但是,如果我在 ABC 中有 (3/5, 1) 并且在 XYZ 中有 (3/4, 1),那么成对的 IN 将不起作用,因为 (3/5, 1) 不在 XYZ 中?

我现在拥有的:

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

你似乎想要exists

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

一般来说,我建议exists无论如何in使用子查询,因为exists通常更容易优化(尽管有些数据库在 中优化in很好)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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