0

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...?

asked Jan 4, 2021 at 20:19

1 Answer 1

3

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
5
  • So, I'm trying to extract only user_id from the DELETE and a separate some_timestamp value. (Edited the question just now.) Commented Jan 4, 2021 at 20:28
  • @GhanashyamS: see my edit Commented Jan 4, 2021 at 20:32
  • Any way I can achieve this...? timestamp (current_timestamp+'1 day') Commented Jan 4, 2021 at 20:45
  • current_timestamp + interval '1 day' will work Commented Jan 4, 2021 at 21:12
  • Perfect! Again! Thank you!! Commented Jan 4, 2021 at 21:23

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.