0

我有以下查询:

select vw.CONFERENCEID, alert.ALERTID, del.CHANNELID, del.DOCUMENTTEMPLATEID,
       vw.starttime, vw.CONFERENCEID, alert.ALERTATTRIBUTEID,
       alert.ALERTCATEGORYID, alert.ATTACHMENT, alert.ATTACHMENTLOCATION,
       alert.ATTACHMENTNAME
  from TBLMCONFERENCE vw,
       TBLMSYSTEMALERTS alert,
       TBLMALERTDELIVERYREL del,
       tblmstandardmaster sm 
 WHERE alert.Alertid = del.alertid
   and sm.masterid = del.CHANNELID
   and alert.SYSTEMGENERATED = 'N'
   and alert.alertid not in (
       select sent.ALERTID
         from TBLMSENTALERTHISTORY sent
        where sent.REFACCOUNTID = vw.conferenceid
          and sent.ALERTID = alert.ALERTID
          and sent.CHANNELID = del.CHANNELID
          and sent.RESETFLAG = 'N')

TBLMSENTALERTHISTORY即使表中存在匹配记录,我也会获取记录。

这个查询有什么问题吗?

4

1 回答 1

1

我不确定你为什么要获取数据,也许你可以用几个表和插入来构建一个小例子。

但是,我可以指出您对运算符的使用不是标准的:在大多数情况下,您不应该使用and运算符NOT IN将子查询连接到主查询(这是多余的)。相反,你会写:INNOT IN

SELECT *
  FROM TBLMCONFERENCE vw,
       TBLMSYSTEMALERTS alert,
       TBLMALERTDELIVERYREL del,
       tblmstandardmaster sm
 WHERE alert.Alertid = del.alertid
   AND sm.masterid = del.CHANNELID
   AND alert.SYSTEMGENERATED = 'N'
   AND (alert.alertid, del.CHANNELID, vw.conferenceid)
       NOT IN (SELECT sent.ALERTID, sent.CHANNELID, sent.conferenceid
                 FROM TBLMSENTALERTHISTORY sent
                WHERE sent.RESETFLAG = 'N')

在您的情况下,您可以直接使用您的子查询NOT EXISTS

SELECT *
  FROM TBLMCONFERENCE vw,
       TBLMSYSTEMALERTS alert,
       TBLMALERTDELIVERYREL del,
       tblmstandardmaster sm
 WHERE alert.Alertid = del.alertid
   AND sm.masterid = del.CHANNELID
   AND alert.SYSTEMGENERATED = 'N'
   AND NOT EXISTS
          (SELECT sent.ALERTID
             FROM TBLMSENTALERTHISTORY sent
            WHERE sent.REFACCOUNTID = vw.conferenceid
              AND sent.ALERTID = alert.ALERTID
              AND sent.CHANNELID = del.CHANNELID
              AND sent.RESETFLAG = 'N')

请注意,由于 NULLNOT IN ,通常和NOT EXISTS不等效。

于 2012-08-29T16:11:23.787 回答