I have a table with id,user_id,timestamp
and I'm trying to INSERT
into the table, the user_id
from a DELETE from table where id=some_number
and timestamp
. So what I'm trying to achieve looks like:
INSERT INTO table (user_id,timestamp)
VALUES (
(DELETE FROM table WHERE id=some_number returning user_id),
current_timestamp+'1 day'
)
RETURNING *;
I am getting a syntax error doing this... Can someone help me here plz...?
Ghanashyam SateeshGhanashyam Sateesh
asked Jan 4, 2021 at 20:19
1 Answer 1
You can use a data modifying common table expression:
with deleted as (
DELETE FROM some_table
WHERE id = 42
returning user_id
)
INSERT INTO other_table(user_id, "timestamp")
select user_id, timestamp '2020-01-04 21:32:34'
from deleted;
answered Jan 4, 2021 at 20:24
user1822user1822
-
So, I'm trying to extract only user_id from the DELETE and a separate some_timestamp value. (Edited the question just now.)Ghanashyam Sateesh– Ghanashyam Sateesh2021年01月04日 20:28:51 +00:00Commented Jan 4, 2021 at 20:28
-
@GhanashyamS: see my edituser1822– user18222021年01月04日 20:32:52 +00:00Commented Jan 4, 2021 at 20:32
-
Any way I can achieve this...?
timestamp (current_timestamp+'1 day')
Ghanashyam Sateesh– Ghanashyam Sateesh2021年01月04日 20:45:11 +00:00Commented Jan 4, 2021 at 20:45 -
current_timestamp + interval '1 day'
will workuser1822– user18222021年01月04日 21:12:49 +00:00Commented Jan 4, 2021 at 21:12 -
Perfect! Again! Thank you!!Ghanashyam Sateesh– Ghanashyam Sateesh2021年01月04日 21:23:15 +00:00Commented Jan 4, 2021 at 21:23
lang-sql