假设我有这样一张表格:
现在,如果我想在冲突的名称上更新电子邮件。
基本上想要追踪活动状态何时更改。那么我可以像这样在单个语句中执行吗?
初始插入:
为了实现我的目的,我正在尝试类似以下的方式:
create schema test;
CREATE TABLE test.customers (
customer_id serial PRIMARY KEY,
name VARCHAR UNIQUE,
email VARCHAR NOT NULL,
active bool NOT NULL DEFAULT TRUE,
is_active_datetime TIMESTAMP(3) NOT NULL DEFAULT'1900-01-01T00:00:00.000Z'::timestamp(3)
updated_datetime TIMESTAMP(3) NOT NULL DEFAULT '1900-01-01T00:00:00.000Z'::timestamp(3),
);
现在,如果我想在冲突的名称上更新电子邮件。
WHERE $tableName.updated_datetime < excluded.updated_datetime
我希望在冲突name
时更新is_active_datetime
,但更新的条件是活动标志已更改。
WHERE customer.active != excluded.active
基本上想要追踪活动状态何时更改。那么我可以像这样在单个语句中执行吗?
初始插入:
insert INTO test.customers (NAME, email)
VALUES
('IBM', 'contact@ibm.com'),
(
'Microsoft',
'contact@microsoft.com'
),
(
'Intel',
'contact@intel.com'
);
为了实现我的目的,我正在尝试类似以下的方式:
select * from test.customers;
INSERT INTO customers (name, email)
VALUES
(
'Microsoft',
'hotline@microsoft.com'
)
ON CONFLICT (name)
DO
UPDATE
SET customers.email = EXCLUDED.email
WHERE $tableName.updated_datetime < excluded.updated_datetime
on CONFLICT (name)
do
update
set is_active_datetime = current_timestamp()
WHERE customer.active != excluded.active ;
这个能做到吗?如何使用这种方法来实现。