【问题标题】:How to use where clause inside NOT EXISTS in sql?如何在 sql 的 NOT EXISTS 中使用 where 子句?
【发布时间】:2012-05-16 00:45:36
【问题描述】:

我需要将 where 条件放在 sql 的 NOT EXISTS 子句中。

在下面需要检查下面sql查询中的重复记录 我需要输入 Date='2012-05-07' 和 SecurityId='52211' 但问题是使用了内部连接,我是新手,不知道如何放置这些 where 子句 请帮忙。

SELECT DISTINCT

    SecurityPriceId

FROM 
    dbo.Indicative Bond
    INNER JOIN 
    dbo.BondPrice BondPrice ON 
        Indicative.SecurityId = BondPrice.SecurityId AND
        BondPrice.SecurityPriceSourceId = @SecurityPriceSourceComposite
WHERE
    Date = @Date -- Date='2012-05-07' like this
    AND
    NOT EXISTS
    (
        SELECT
            'z'
        FROM
            dbo.Reporting_BondPrices
    WHERE
        Reporting_BondPrices.SecurityId = BondPrice.SecurityId AND
        Reporting_BondPrices.Date = BondPrice.Date 
        --how can i put Date='2012-05-07' and SecurityId='52211'                 
    )

【问题讨论】:

    标签: sql sql-server


    【解决方案1】:

    根据你的更新,我想(??)你想要这个?

    SELECT DISTINCT
    
        BondPrice.SecurityPriceId
    
    FROM 
        dbo.Reporting_BondIndicative Reporting_BondIndicative
        INNER JOIN 
        dbo.BondPrice BondPrice ON 
            Reporting_BondIndicative.SecurityId = BondPrice.SecurityId AND
            BondPrice.SecurityPriceSourceId = @SecurityPriceSourceComposite
    WHERE
        BondPrice.Date = @Date -- BondPrice.Date='2012-05-07' like this
        AND
        NOT EXISTS
        (
            SELECT
                'z'
            FROM
                dbo.Reporting_BondPrices
        WHERE
            Reporting_BondPrices.SecurityId = BondPrice.SecurityId AND
            Reporting_BondPrices.Date = BondPrice.Date 
            --how can i put Date='2012-05-07' and SecurityId='52211'                 
            --simply put them in with and
            and Reporting_BondPrices.SecurityId='52211' and Reporting_BondPrices.Date='20120507'
        )
    

    早期尝试解码您的问题:

    你可以给你的表起别名,像这样:

    select ...
    from table as t1  --t1 will be the outer table
    where not exists(select ...
                     from table as t1  --t2 will be the inner table
                     where t1.column1=t2.column1 and t1.column2<>t2.column2)
    

    【讨论】:

    • 我只需要把这两个条件放在里面我该怎么做?
    • sql 查询没有得到任何答案你确定最后添加的and 语句是正确的吗?我希望在 where 子句中使用这个条件,但我需要把它放在哪里我不知道 :(
    猜你喜欢
    • 2013-04-17
    • 2016-01-09
    • 2019-05-18
    • 2011-02-03
    • 2013-02-10
    • 1970-01-01
    • 1970-01-01
    • 2018-01-28
    • 2020-01-28
    相关资源
    最近更新 更多