在where的或子句中等于不等于

时间:2018-07-31 05:20:30

标签: sql sql-server tsql

以下查询是什么意思?

    SELECT 
        bid
      , chapter_id 
    FROM
        tblchapter c 
      , tblbook b
    WHERE 
      (b.bid = c.bid OR b.bid <> c.bid)

下面是查询,行之间的代码是我要寻找的。请帮我。这需要更多时间。如何重写:

    SELECT           
            IDENTITY(INT, 1,1) AS rownum,           
            b.bid AS bid,          
            b.book_id AS book_id,       
            Chapter_S200Received_Date=CONVERT(VARCHAR,chap.chapter_s200_recd_dt,105)+' '+CONVERT(VARCHAR,chap.chapter_s200_recd_dt,108),          
            Chapter_S200Due_Date=CONVERT(VARCHAR,chap.chapter_s200_due_dt,105)+' '+CONVERT(VARCHAR,chap.chapter_s200_due_dt,108),             
            DATEDIFF(dd,getdate(),chap.chapter_s200_due_dt)AS jsScheduleprogress,        
            editor_name = (SELECT user_name FROM EMS_DB.dbo.tblUser WHERE user_id=chap.chapter_s200_editor),          
            isnull(b.book_ce_level,chap.ce_level) as ce_level,          
            DATEADD(day, 2, b.book_s600_due_dt) AS book_s600_due_dt          

    FROM           
            tblBook AS b LEFT OUTER JOIN tblBookStatusUpload AS pp 
            ON b.bid=pp.bid LEFT OUTER JOIN tblLocation AS Loc 
            ON b.book_location=loc.location_id  LEFT OUTER JOIN tblSegregatingCellName AS cel
            ON cel.cell_id=b.book_proof_cell_id LEFT OUTER JOIN 
------------------------------------------------------------------------------
            (SELECT           
            c.bid,          
            MAX(c.chapter_s200_recd_dt) AS chapter_s200_recd_dt ,          
            MIN(c.chapter_s200_due_dt) AS chapter_s200_due_dt,          
            (SELECT MAX (ce_level) FROM tblchapter c) AS ce_level,          
            (SELECT TOP 1 chapter_s200_editor FROM tblchapter where chapter_s200_editor is not null AND bid=c.bid) AS chapter_s200_editor   

            FROM           
            tblbook AS b INNER JOIN tblchapter AS c 
            ON b.bid=c.bid  INNER JOIN tblChapterStatusDetails AS s     
            ON (c.bid=s.bid or c.bid<>s.bid)   
            WHERE           

            c.chapter_s200_recd_dt >='2011-04-01' AND          
            c.chapter_s200_recd_dt is not null AND            
            c.chapter_s200_due_dt is not null AND           
            c.chapter_s200_finish_dt is  null          
            and  
            s.process_out_dt IS NULL)  

            group by          
            c.bid) AS chap 
------------------------------------------------------------------------------     
            ON    chap.bid=b.bid          
            where           
            b.bid=chap.bid AND             
            b.book_id is not null  order by b.bid

有没有人可以帮助我。

4 个答案:

答案 0 :(得分:0)

子句ON (c.bid=s.bid or c.bid<>s.bid)与下面的and s.process_out_dt IS NULL)组合意味着,对于每个章节,您将获得process_out_dt所在的每个状态值(而不仅仅是您正在查看的章节/书的状态)为null,而c.bid不为null。

第二个子句中明确定义了s.process_out_dt不为null,而c.bid为null将同时使=和<>测试失败,因此也将其排除在外。

所以这显然是错误的。试图弄清它的目的是什么,只是错误,没有任何价值,因为看过它的每个人都可以看到它毫无意义。

查看一下tblChapterStatusDetail和tblChapter之间的外键,因为它不太可能只是出价(Book_id?)。很可能是单独的Chapter_id列,或者是book_id,chapter_id的组合键。没有表定义很难知道。

答案 1 :(得分:0)

上面的查询基本上将执行交叉连接,查询的输出将返回两个表中的所有行,其中bid不为空。

输出中返回的行数为:

  

count(b.bid)* count(c.bid)

答案 2 :(得分:0)

您可以使用CROSS JOIN

SELECT 
    bid
  , chapter_id 
FROM
    tblchapter c 
  CROSS JOIN tblbook b

答案 3 :(得分:0)

问题不完整。但是,您可以运行此代码,然后检查是否要这样做。

SELECT c.bid
    ,MAX(c.chapter_s200_recd_dt) AS chapter_s200_recd_dt
    ,MIN(c.chapter_s200_due_dt) AS chapter_s200_due_dt
FROM tblbook AS b 
INNER JOIN tblchapter AS c ON b.bid = c.bid
INNER JOIN tblChapterStatusDetails AS s ON c.bid = s.bid
GROUP BY b.bid