博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
postgres 从修改行返回数据
阅读量:2342 次
发布时间:2019-05-10

本文共 1648 字,大约阅读时间需要 5 分钟。

Returning Data From Modified Rows

Sometimes it is useful to obtain data from modified rows while they are being manipulated. The INSERT, UPDATE, and DELETE commands all have an optional RETURNING clause that supports this. Use of RETURNING avoids performing an extra database query to collect the data, and is especially valuable when it would otherwise be difficult to identify the modified rows reliably.

The allowed contents of a RETURNING clause are the same as a SELECT command’s output list (see Section 7.3). It can contain column names of the command’s target table, or value expressions using those columns. A common shorthand is RETURNING *, which selects all columns of the target table in order.

In an INSERT, the data available to RETURNING is the row as it was inserted. This is not so useful in trivial inserts, since it would just repeat the data provided by the client. But it can be very handy when relying on computed default values. For example, when using a serial column to provide unique identifiers, RETURNING can return the ID assigned to a new row:

CREATE TABLE users (firstname text, lastname text, id serial primary key);INSERT INTO users (firstname, lastname) VALUES ('Joe', 'Cool') RETURNING id;

The RETURNING clause is also very useful with INSERT … SELECT.

In an UPDATE, the data available to RETURNING is the new content of the modified row. For example:

UPDATE products SET price = price * 1.10  WHERE price <= 99.99  RETURNING name, price AS new_price;

In a DELETE, the data available to RETURNING is the content of the deleted row. For example:

DELETE FROM products  WHERE obsoletion_date = 'today'  RETURNING *;

转载地址:http://beyvb.baihongyu.com/

你可能感兴趣的文章
资深影迷不可不知的宽高比:Aspect Ratio 电影画面比例
查看>>
MacBook Pro 外接显示器设置竖屏
查看>>
X264的参考帧设置
查看>>
三种帧的说明
查看>>
感知视频编码
查看>>
深度学习 vs 机器学习 vs 模式识别
查看>>
Tone mapping进化论
查看>>
XAVC
查看>>
详解HDR的三个标准——HLG/HDR10/Dolby Vision
查看>>
流言终结者 1080P全高清都等于高画质?
查看>>
PSNR指标值
查看>>
灰度图像-图像增强 中值滤波
查看>>
两种HDR格式(HLG, HDR10)的理解
查看>>
视频主观质量对比工具(Visual comparision tool based on ffplay)
查看>>
HDMI 接口及CEC信号
查看>>
H.264专利介绍
查看>>
YUV格式小结
查看>>
log4j2.xml实用例子
查看>>
Dockerfile中的CMD和ENTRYPOINT有什么区别?
查看>>
jQuery提示和技巧
查看>>