今天开发问到,postgressql里面有没有像mysql那样插入一个值后返回插入的值,这个是有的,而且有更强的扩展性。

 示例:

[postgres@localhost ~]$ psql
psql (9.2.4)
Type "help" for help.
postgres=# create table t_kenyon(id int,vname varchar(30),remark text);
CREATE TABLE
postgres=# insert into t_kenyon(id,vname) values(1,'test_kenyon') returning id;
id
----
1
(1 row) INSERT 0 1
postgres=# insert into t_kenyon(id,vname) select generate_series(1,5),'Kenyon here' returning id;
id
----
1
2
3
4
5
(5 rows) INSERT 0 5

扩展:
 a.返回更多的insert内容

postgres=# insert into t_kenyon(id,vname) select generate_series(6,8),'Kenyon here' returning id,vname;
id | vname
----+-------------
6 | Kenyon here
7 | Kenyon here
8 | Kenyon here
(3 rows) INSERT 0 3 postgres=# insert into t_kenyon(id,vname,remark) select generate_series(9,11),'Kenyon here','KENYON GOOD BOY!' returning *;
id | vname | remark
----+-------------+------------------
9 | Kenyon here | KENYON GOOD BOY!
10 | Kenyon here | KENYON GOOD BOY!
11 | Kenyon here | KENYON GOOD BOY!
(3 rows) INSERT 0 3

b.返回delete掉的数据

postgres=# select * from t_kenyon;
id | vname | remark
----+-------------+------------------
1 | test_kenyon |
1 | Kenyon here |
2 | Kenyon here |
3 | Kenyon here |
4 | Kenyon here |
5 | Kenyon here |
6 | Kenyon here |
7 | Kenyon here |
8 | Kenyon here |
9 | Kenyon here | KENYON GOOD BOY!
10 | Kenyon here | KENYON GOOD BOY!
11 | Kenyon here | KENYON GOOD BOY!
(12 rows) postgres=# delete from t_kenyon where id >9 returning id,vname;
id | vname
----+-------------
10 | Kenyon here
11 | Kenyon here
(2 rows) DELETE 2
postgres=# delete from t_kenyon where id <5 returning *;
id | vname | remark
----+-------------+--------
1 | test_kenyon |
1 | Kenyon here |
2 | Kenyon here |
3 | Kenyon here |
4 | Kenyon here |
(5 rows) DELETE 5
postgres=# select * from t_kenyon;
id | vname | remark
----+-------------+------------------
5 | Kenyon here |
6 | Kenyon here |
7 | Kenyon here |
8 | Kenyon here |
9 | Kenyon here | KENYON GOOD BOY!
(5 rows)

c.返回update掉的数据

postgres=# update t_kenyon set remark = 'kenyon bad boy!' where id <7 returning id,remark;
id | remark
----+-----------------
5 | kenyon bad boy!
6 | kenyon bad boy!
(2 rows) UPDATE 2

mysql的last_insert_id使用有诸多限制和注意的地方,如字段需要auto_increment,一个SQL插入多个值的时候只会返回第一个id值,此不再叙述。

PostgresSQL用returning实现mysql的last_insert_id的更多相关文章

  1. mysql select last_insert_id()函数返回的值

    mysql)); 创建表j 插入数据 mysql> insert into j(name) values('wanggiqpg'); Query OK, row affected (0.00 s ...

  2. mysql利用LAST_INSERT_ID实现id生成器

    首先了解 LAST_INSERT_ID LAST_INSERT_ID 有自己的存储空间,能存一个数字 不带参数时返回最近insert的那行记录的自增字段值.带参数时会将自己存储的数字刷成参数给定的值 ...

  3. MySQL的mysql_insert_id和LAST_INSERT_ID(转)

    本文介绍的是mysql中last_insert_id和mysql_insert_id的区别 1 mysql_insert_id 一.PHP获取MYSQL新插入数据的ID mysql_insert_id ...

  4. Mysql函数:Last_insert_id()语法讲解

    Mysql函数可以实现许多我们需要的功能,下面介绍的Mysql函数Last_insert_id()就是其中之一,希望对您学习Mysql函数能有所帮助. 自动返回最后一个INSERT或 UPDATE 查 ...

  5. mysql LAST_INSERT_ID 使用与注意事项

    在使用MySQL时,若表中含自增字段(auto_increment类型),则向表中insert一条记录后,可以调用last_insert_id()来获得最近insert的那行记录的自增字段值 $mdb ...

  6. mysql insert一条记录后怎样返回创建记录的主键id,last_insert_id(),selectkey

    mysql插入数据后返回自增ID的方法 mysql和oracle插入的时候有一个很大的区别是,oracle支持序列做id,mysql本身有一个列可以做自增长字段,mysql在插入一条数据后,如何能获得 ...

  7. MySQL的mysql_insert_id和LAST_INSERT_ID

    摘要:mysql_insert_id和LAST_INSERT_ID二者作用一样,均是返回最后插入值的ID 值 1 mysql_insert_id 一.PHP获取MYSQL新插入数据的ID  mysql ...

  8. mysql插入数据后返回自增ID的方法,last_insert_id(),selectkey

    mysql插入数据后返回自增ID的方法 mysql和oracle插入的时候有一个很大的区别是,oracle支持序列做id,mysql本身有一个列可以做自增长字段,mysql在插入一条数据后,如何能获得 ...

  9. mysql LAST_INSERT_ID详解

    http://blog.sina.com.cn/s/blog_5b5460eb0100nwvo.html LAST_INSERT_ID() LAST_INSERT_ID(expr) 自动返回最后一个I ...

  10. MySQL 获取最后插入的ID LAST_INSERT_ID用法

    LAST_INSERT_ID() 自动返回最后一个INSERT或 UPDATE 查询中 AUTO_INCREMENT列设置的第一个表发生的值. MySQL的LAST_INSERT_ID的注意事项: 第 ...

随机推荐

  1. 【Phoenix】简介、架构、存储、入门、常用表操作、表的映射方式、配置二级索引

    一.Phoenix简介 1.定义 构建在 HBase 之上的开源 SQL 层 可以使用标准的 JDBC API 去建表, 插入数据和查询 HBase 中的数据 避免使用 HBase 的客户端 API ...

  2. 现代 CSS 之高阶图片渐隐消失术

    在过往,我们想要实现一个图片的渐隐消失.最常见的莫过于整体透明度的变化,像是这样: <div class="img"></div> div { width: ...

  3. avue框架 拼接后端返回的数据到table中

    根据要求展示下列详细地址情况: 后端返回的数据: 具体实现步骤: { label: "详细地址", prop: "buildingName", display: ...

  4. css实习滤镜效果(背景图模糊)

    模糊实例 图片使用高斯模糊效果: img { -webkit-filter: blur(5px); /* Chrome, Safari, Opera */ filter: blur(5px); } c ...

  5. Spring IoC的一些知识点

    在日常开发中,接触得比较多的算是Spring生态了,Spring Ioc是Spring Framework重要的组成部分,下面整理了一些Spring Ioc的知识点. 1. 什么是IoC IoC(In ...

  6. Error: Could not get apiVersions from Kubernetes

    问题 部署pod时遇到问题 # helm install chart.tgz Error: Could not get apiVersions from Kubernetes: unable to r ...

  7. Codeforces Hello 2023 CF 1779 A~F 题解

    点我看题 A. Hall of Fame 先把不用操作就合法的情况判掉.然后发现交换LL,RR,RL都是没用的,只有交换LR是有用的,这样可以照亮相邻的两个位置.所以我们就是要找到一个位置i,满足\( ...

  8. 如何在Github上创建一个新仓库

    Hi,欢迎大家在有空的时候做客[江涛学编程],这里是2023年的第6篇原创文章,新年新气象,在这里我祝读者朋友们都好好的, 老规矩,拍拍手,上菜. 今天没有啥东西要跟家人们分享,就两个字,看图!!! ...

  9. 聊聊web漏洞挖掘第一期

    之前写2022年度总结的时候,有提到要给大家分享漏洞挖掘技巧.这里简单分享一些思路,更多的内容需要大家举一反三. 文章准备昨晚写的,昨天晚上出去唱歌,回来太晚了,耽搁了.昨天是我工作的last day ...

  10. Potree 003 基于Potree Desktop创建自定义工程

    1.第三方js库 第三方库js库选择dojo,其官网地址为https://dojotoolkit.org/,git地址为https://github.com/dojo/dojo,demo地址为http ...