1

我看了看 SQL-92 标准,然后看了看有人整理的 SQL-92 语法,但看不懂。

正如SQL Server 文档提醒我们的那样,在某些情况下,表达式应该被显式分组:

默认情况下,SQL Server 2005 从左到右计算包含 UNION 运算符的语句。使用括号指定评估顺序。例如,以下语句不等价:

/* First statement. */
SELECT * FROM TableA
UNION ALL
(   SELECT * FROM TableB
   UNION
   SELECT * FROM TableC
)
GO

/* Second statement. */
(SELECT * FROM TableA
 UNION ALL
 SELECT * FROM TableB
)
UNION
SELECT * FROM TableC)
GO

在第一个语句中,在 TableB 和 TableC 之间的联合中消除了重复项。在该集合和 TableA 之间的联合中,不会消除重复项。在第二个语句中,重复项包含在 TableA 和 TableB 之间的联合中,但在随后与 TableC 的联合中被消除。ALL 对这个表达式的最终结果没有影响。

但我发现 MySQL 和 SqlLite 不支持它(供参考,Oracle、SQL Server、Postgres 和 DB2 支持。)

那么如何检查规格是否允许呢?

4

1 回答 1

0

摘自SQL-92 语法,摘自 SQL-92 标准并带有超链接:

<direct select statement: multiple rows>    ::=   <query expression> [ <order by clause> ]

<query expression>    ::=   <non-join query expression> | <joined table>

<non-join query expression>    ::= 
         <non-join query term>
     |     <query expression>  UNION  [  ALL  ] [ <corresponding spec> ] <query term>
     |     <query expression>  EXCEPT  [  ALL  ] [ <corresponding spec> ] <query term>

<joined table>    ::= 
         <cross join>
     | <qualified join>
     | <left paren> <joined table> <right paren>

<non-join query term>    ::= 
         <non-join query primary>
     |     <query term>  INTERSECT  [  ALL  ] [ <corresponding spec> ] <query primary>

<query term>    ::=   <non-join query term> | <joined table>

<cross join>    ::= 
         <table reference>  CROSS  JOIN <table reference>

<qualified join>    ::= 
         <table reference> [  NATURAL  ] [ <join type> ]  JOIN <table reference> [ <join specification> ]

<non-join query primary>    ::=   <simple table> | <left paren> <non-join query expression> <right paren>

<query primary>    ::=   <non-join query primary> | <joined table>

<table reference>    ::= 
         <table name> [ <correlation specification> ]
     | <derived table> <correlation specification>
     | <joined table>

<simple table>    ::= 
         <query specification>
     |     <table value constructor>
     |     <explicit table>

<query specification>    ::= 
          SELECT  [ <set quantifier> ] <select list> <table expression>

<table expression>    ::= 
         <from clause>
         [ <where clause> ]
         [ <group by clause> ]
         [ <having clause> ]

<direct select statement: multiple rows> 是我们将要匹配的规则。我们检查

SELECT * FROM p匹配 <query specification>、<simple table>、<non-join query primary>、<non-join query term>、<non-join query expression>、<query expression>

SELECT * FROM p UNION SELECT * FROM q匹配 <非连接查询表达式>

(SELECT * FROM p UNION SELECT * FROM q)匹配 <non-join query primary>、<non-join query term>、<non-join query expression>、<query expression>

(SELECT * FROM p UNION SELECT * FROM q) UNION SELECT * FROM r现在匹配 <non-join query expression> 和 <query expression>。

因此它匹配 <direct select statement: multiple rows>,因此它是一个有效的 SQL-92 语句。

类似地,SELECT * FROM p UNION (SELECT * FROM q UNION SELECT * FROM r)(SELECT * FROM p)(SELECT * FROM p UNION SELECT * FROM q)evenp CROSS JOIN q是有效的 SQL-92 选择语句。

于 2013-04-11T21:28:52.443 回答