postgresql equivalent to $mysqli->insert_id

跟風遠走 提交于 2019-12-24 14:19:16

问题


Is there an equivalent to $mysqli->insert_id in postgresql 9.3? I am importing into one table, but I need to take the last primary key created in the table and apply it as a foreign key in the second table? Table 1 ----> Table 2 to complete a record.

A bit more info:

$sql = "INSERT INTO sheet_tbl (site_id,  eventdate) VALUES ('$_POST[site_id]','$_POST[eventdate]') returning id";

**Using the sql above, i insert my record, how would I then utilise this in my next SQL insert, If i use returning id, where is this ID stored, How do I use it on another INSERT statement?

 $sql1 = "INSERT INTO record_tbl (line, taxom_id, number_of_taxom, sheet_id) VALUES (1,1,'$_POST[cased_numbers]','$_POST[sheet_id]')";
  $result = pg_query($sql1);

回答1:


If you are need the PK of the row you just inserted (or any expression using the table's columns), you can use the RETURNING clause. Docs: RETURNING clause

Eg.

test=# create table a (id serial, val text);
NOTICE:  CREATE TABLE will create implicit sequence "a_id_seq" for serial column "a.id"
CREATE TABLE

test=# insert into a (val) values ('foo') returning id;
 id 
----
  1



回答2:


In PostgreSQL, the primary key values usually come from a sequence as a best practice. You can use SELECT currval('sequence_name'); to get the last value of a particular sequence or SELECT lastval() to get the last value of any sequence. See the official docs for details.

You can see an example of currval working in this SQL fiddle. Be aware that it is required use the sequence already in the session, as the insert would do. Calling currval when the sequence hasn't been used in the current session is an error.

If you are using PDO in PHP, the lastInsertId method there is supported by PostgreSQL, but requires you to provide the sequence ID. More docs and discussion about PDO::lastInsertId are available.

As @bma suggests, using a RETURNING clause on your INSERT statement is a good way to get back the value of the primary key of the row you just inserted:

insert into a (val) values ('foo') returning id;

Also, your syntax appears that it could be vulnerable to a SQL injection attack, because you are passing unvalidated input directly into the database without escaping it. Consider using pg_query_params or some other alternative to make sure your inputs are properly escaped.




回答3:


Actualy you can do it in one query:

WITH sheet AS(
    INSERT INTO sheet_tbl (site_id,  eventdate) VALUES ('$_POST[site_id]','$_POST[eventdate]') returning id
)
INSERT INTO record_tbl select 1,1,'$_POST[cased_numbers]',id FROM sheet;


来源:https://stackoverflow.com/questions/17860622/postgresql-equivalent-to-mysqli-insert-id

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!