Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/9.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 带查询的Postgres_Sql_Postgresql_Common Table Expression - Fatal编程技术网

Sql 带查询的Postgres

Sql 带查询的Postgres,sql,postgresql,common-table-expression,Sql,Postgresql,Common Table Expression,为了简单起见,假设我的表有一列,其中total是一个整数。在这里,我希望foo子查询将total增加1,将bar减少1,从而得到相同的起始值。但是,当我运行此查询时,总值将递增。为什么会这样,我如何解决这个问题 WITH foo AS ( UPDATE table SET total = total - 1 WHERE id = 10 RETURNING * ), bar AS ( UPDATE table SET total = total + 1 WHERE id = 10 RETURNI

为了简单起见,假设我的表有一列,其中total是一个整数。在这里,我希望foo子查询将total增加1,将bar减少1,从而得到相同的起始值。但是,当我运行此查询时,总值将递增。为什么会这样,我如何解决这个问题

WITH 
foo AS (
UPDATE table SET total = total - 1 WHERE id = 10
RETURNING *
),
bar AS (
UPDATE table SET total = total + 1 WHERE id = 10
RETURNING *
)
SELECT * FROM table

它在SQL FIDLE:上正常工作。我无法重现你的问题。因为你的
正在增加,而你的
foo
正在减少1,你忘了提供你的Postgres版本。我使用的是9.4版。另外,我希望该值保持不变,但它是递增的。@dmwong2268您的查询工作正常。请提供实际表的详细信息和实际查询。