如何在PostgreSQL查询中使用返回值

时间:2018-04-15 08:41:52

标签: database postgresql postgresql-9.6

PostgreSQL 9.6。

我想在同一个查询中使用返回值,但稍后会是这样的:

WITH new_track AS (
    INSERT INTO eco.tracks (id, first_time, last_time) 
    VALUES ('71C055', '2018/04/10', '2018/04/10') RETURNING track
)

SELECT * FROM eco.new_tracks WHERE track = (SELECT track from new_track);

有可能吗?

1 个答案:

答案 0 :(得分:0)

您可以使用RETURNING *

WITH new_track AS (
    INSERT INTO eco.tracks (id, first_time, last_time) 
    VALUES ('71C055', '2018/04/10', '2018/04/10') RETURNING *
)
SELECT * FROM new_track;

<强> DBFiddle Demo

修改

  

我的坏。第二个表是另一个表,请查看更新版本

WITH new_track AS (
    INSERT INTO eco.tracks (id, first_time, last_time) 
    VALUES ('71C055', '2018/04/10', '2018/04/10') RETURNING id
)
SELECT * FROM eco.new_tracks t
WHERE t.col_name IN (SELECT id FROM new_track);