加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > MsSql教程 > 正文

是否可以比较SQL查询的`WHERE`子句中的*元组*?

发布时间:2020-12-12 08:43:42 所属栏目:MsSql教程 来源:网络整理
导读:是否可以在SQL查询的WHERE子句中比较元组(thanks,a_horse_with_no_name)?那样,我可以转换这个: /* This is actually a sub-sub-sub-query in the middle * * of an incredibly complex stored procedure. */SELECT ISNULL(SUM(DT.DetailField),0)FROM Detai
是否可以在SQL查询的WHERE子句中比较元组(thanks,a_horse_with_no_name)?那样,我可以转换这个:
/* This is actually a sub-sub-sub-query in the middle *
 * of an incredibly complex stored procedure.         */
SELECT ISNULL(SUM(DT.DetailField),0)
FROM   DetailTable DT
WHERE  DT.ForeignKey = ...
AND    EXISTS (/* I know this sub-sub-sub-sub-query *
                * will return at most one row.      */
               SELECT 'X'
               FROM   HeaderTable HT
               WHERE  HT.HeaderKey    = DT.HeaderKey
               AND    HT.HeaderField1 = ...
               AND    HT.HeaderField2 = ...)

进入类似的东西:

SELECT ISNULL(SUM(DetailField),0)
FROM   DetailTable DT
WHERE  DT.ForeignKey = ...
AND    (SELECT HT.HeaderField1,HT.HeaderField2
        FROM   HeaderTable HT
        WHERE  HT.HeaderKey = DT.HeaderKey) = (...,...)

解决方法

Goran的回答对我来说最好,我投了赞成票.只是为了添加另一个选项,因为您使用的是SQL Server,从子查询中获取多个列的灵活方法是外部应用.你可以比较两个值(一个元组),如:
select  *
from    SomeTable t1
outer apply
        (
        select  *
        from    SomeOtherTable t2
        where   t1.Stuff = t2.Unit
        ) sub1
outer apply
        (
        select  *
        from    YetAnotherTable t3
        where   t1.Stuff = t3.jetser
        ) sub2
where   sub1.col1 = sub2.col1
        and sub1.col2 = sub2.col2

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读