WITH
queryの結果を参照して新しいqueryを発行したい場合WITH句を使うことができる
WITH句を使うことで一時的な変数を作ることができる
code:sample.sql
WITH temp_post AS (
INSERT INTO posts (
title,
body,
user_id
)
VALUES (
'TEST',
'This is test body.',
1
)
RETURNING title, body, user_id
)
SELECT id, username, nickname FROM temp_post, users
WHERE temp_post.user_id = users.id;
table:result
title body user_id id username nickname
TEST This is test body. 1 1 testuser testusernick