最新文章专题视频专题问答1问答10问答100问答1000问答2000关键字专题1关键字专题50关键字专题500关键字专题1500TAG最新视频文章推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37视频文章20视频文章30视频文章40视频文章50视频文章60 视频文章70视频文章80视频文章90视频文章100视频文章120视频文章140 视频2关键字专题关键字专题tag2tag3文章专题文章专题2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章专题3
当前位置: 首页 - 科技 - 知识百科 - 正文

Postgres用returning实现mysql的last_insert_id

来源:动视网 责编:小采 时间:2020-11-09 16:07:43
文档

Postgres用returning实现mysql的last_insert_id

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


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 <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值,此不再叙述。

文档

Postgres用returning实现mysql的last_insert_id

Postgres用returning实现mysql的last_insert_id:Postgres用returning实现mysql的last_insert_id 今天开发问到,postgres里面有没有像mysql那样插入一个值后返回插入的值,这个是有的,而且有更强的扩展性。 示例: [postgres@localhost ~]$ psql psql (9.2.4) Type help
推荐度:
标签: id 实现 mysql
  • 热门焦点

最新推荐

猜你喜欢

热门推荐

专题
Top