[英]SQL--Pairwise condition
Does SQL have any pairwise conditions in the WHERE clause that aren't using IN? SQL 在 WHERE 子句中是否有任何不使用 IN 的成对条件? My use case is I have two tables: (Date, ID) from table ABC and (LaunchDate, ID) from table XYZ.我的用例是我有两个表:表 ABC 中的 (Date, ID) 和表 XYZ 中的 (LaunchDate, ID)。 My condition is that ABC.ID is in XYZ.ID and ABC.Date>=XYZ.LaunchDate (for that corresponding ID).我的条件是 ABC.ID 在 XYZ.ID 和 ABC.Date>=XYZ.LaunchDate (对应的 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?但是,如果我在 ABC 中有 (3/5, 1) 并且在 XYZ 中有 (3/4, 1),那么成对的 IN 将不起作用,因为 (3/5, 1) 不在 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
:你似乎想要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
).一般来说,我建议exists
无论如何in
使用子查询,因为exists
通常更容易优化(尽管有些数据库在 中优化in
很好)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.