Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 为什么union不能处理语句?_Sql_Postgresql_Postgresql 9.4 - Fatal编程技术网

Sql 为什么union不能处理语句?

Sql 为什么union不能处理语句?,sql,postgresql,postgresql-9.4,Sql,Postgresql,Postgresql 9.4,这将产生一个错误: with a1 as (select 2), a2 as (select 3) a1 union a2; -- ERROR: syntax error at or near "a1" 但这不会: (select 2) union (select 3); /* ?column? ---------- 2 3 (2 rows) */ 发生了什么事?因为第一个查询的格式不正确,并且有一些语法错误,SELECT语句在哪里?a1和a2应该是什么

这将产生一个错误:

with a1 as (select 2), a2 as (select 3)
a1 union a2;
-- ERROR:  syntax error at or near "a1"
但这不会:

(select 2) union (select 3);
/*
 ?column? 
----------
        2
        3
(2 rows)
*/

发生了什么事?

因为第一个查询的格式不正确,并且有一些语法错误,
SELECT
语句在哪里?
a1和a2
应该是什么

这应该起作用:

with a1 as (select 2), a2 as (select 3)
SELECT * FROM a1
UNION
SELECT * FROM a2;
这通常用于进行复杂的计算,并将其简化。不适用于
1联合2

这基本上是创建要使用的派生表,在
with
部分之后,查询开始,因此应该将其格式化为正常的
select/update/delete
查询,但是
a1和a2
将可供使用