Postgres用returning实现mysql的last_insert_id

php中文网
发布: 2016-06-07 14:58:32
原创
1766人浏览过

postgres用returning实现mysql的last_insert_id 今天开发问到,postgres里面有没有像mysql那样插入一个值后返回插入的值,这个是有的,而且有更强的扩展性。 示例: [postgres@localhost ~]$ psql psql (9.2.4) Type help for help. postgres=# create table

postgres用returning实现mysql的last_insert_id

 

  今天开发问到,postgres里面有没有像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

 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

 id |     remark      

----+-----------------

  5 | kenyon bad boy!

  6 | kenyon bad boy!

(2 rows)

 

UPDATE 2

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

最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
相关标签:
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号