相当于$ mysqli-> insert_id的postgresql [英] postgresql equivalent to $mysqli->insert_id
问题描述
postgresql 9.3中是否有与$ mysqli-> insert_id等效的文件?我要导入一个表,但是我需要使用在表中创建的最后一个主键,并将其作为第二个表中的外键应用?表1 ---->表2完成记录.
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.
更多信息:
$sql = "INSERT INTO sheet_tbl (site_id, eventdate) VALUES ('$_POST[site_id]','$_POST[eventdate]') returning id";
**使用上面的sql,我插入我的记录,然后如何在下一个SQL插入中使用它,如果我使用返回ID,该ID存储在哪里,如何在另一个INSERT语句上使用它? /p>
**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);
推荐答案
如果您需要刚插入的行的PK(或使用表列的任何表达式),则可以使用RETURNING子句.文件: RETURNING子句
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
例如.
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
这篇关于相当于$ mysqli-> insert_id的postgresql的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!