有0 = COUNT

时间:2018-05-21 16:05:51

标签: sql having

HAVING 0 = COUNT(1)) AS a

这是什么意思?

SELECT outr.*
  FROM Orders outr
 WHERE
EXISTS (   SELECT *
                    FROM (   SELECT ol.Orders_ID
                               FROM Orderlines AS ol
                              WHERE Status NOT IN ( 5, 6 )
                              GROUP BY ol.Orders_ID
                             HAVING 0 = COUNT(1)) AS a
                   WHERE outr.Orders_ID = a.Orders_ID)

1 个答案:

答案 0 :(得分:1)

AS a是派生表的别名。简化为:

SELECT *
FROM (SELECT Orders_ID
      FROM Orderlines
      GROUP BY Order_ID
      HAVING 0 = COUNT(1)
     ) AS a

HAVING 0 = COUNT(1)表示查询只返回计数为0的Orders_ID。请注意COUNT(1)COUNT(*)相同。

它没有任何意义,因为Orders_ID永远不存在并且计数为0.

因此,派生表永远不会返回任何内容,因为它位于外部查询的EXISTS子句中,外部查询也不会。