postgres update column with value from another table
UPDATE t1
set "Column" = t2.column
from t2
where t2.id = t1."Id";
postgres update column with value from another table
UPDATE t1
set "Column" = t2.column
from t2
where t2.id = t1."Id";
postgres updatet_at field
-- Step 1: Create the function
CREATE OR REPLACE FUNCTION trigger_set_timestamp()
RETURNS TRIGGER AS $$
BEGIN
NEW.updated_at = NOW();
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
-- Step 2: Create the table
CREATE TABLE my_table (
id SERIAL NOT NULL PRIMARY KEY,
content TEXT,
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);
-- Step 3: Create the trigger
CREATE TRIGGER set_timestamp
BEFORE UPDATE ON my_table
FOR EACH ROW
EXECUTE PROCEDURE trigger_set_timestamp();
-- Step 4: Profit
-- Now we can insert and update rows in the table, and both
-- the created_at and updated_at columns will be saved correctly :)
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us