Liquibase 在不存在的地方插入 select
Liquibase insert select where not exists
我想将 table2
中的多行插入到 table1
中。问题是我在 table2
和 table1
中有一个同名字段,如果该字段中已经有具有相同值的记录,我不想插入数据。现在我有这样的东西:
insert into table1 (id, sameField, constantField, superFied)
select gen_random_uuid(), "sameField", 'constant', "anotherField"
from table2;
我想我需要做这样的事情:
insert into table1 (id, sameField, constantField, superFied)
select gen_random_uuid(), "sameField", 'constant', "anotherField"
from table2
where not exists ... ?
如果我想要这个逻辑,我需要写什么而不是 ?
:当从 table2
? DBMS 是 Postgres。
您可以使用sub-query查看记录是否存在。您将需要定义应该唯一的列。
create table table2(
id varchar(100),
sameField varchar(25),
constant varchar(25),
superField varchar(25)
);
insert into table2 values
(gen_random_uuid(),'same1','constant1','super1'),
(gen_random_uuid(),'same2','constant2','super2')
✓
2 行受影响
create table table1(
id varchar(100),
sameField varchar(25),
constant varchar(25),
superField varchar(25)
);
insert into table1 values
(gen_random_uuid(),'same1','constant1','super1');
✓
1 行受影响
insert into table1 (id, sameField, constant, superField)
select uuid_in(md5(random()::text || clock_timestamp()::text)::cstring),
t2.sameField, 'constant', t2.superField
from table2 t2
where sameField not in (select sameField from table1)
1 行受影响
select * from table1;
select * from table2;
id | samefield | constant | superfield
:----------------------------------- | :-------- | :-------- | :---------
4cf10b1c-7a3f-4323-9a16-cce681fcd6d8 | same1 | constant1 | super1
d8cf27a0-3f55-da50-c274-c4a76c697b84 | same2 | constant | super2
id | samefield | constant | superfield
:----------------------------------- | :-------- | :-------- | :---------
c8a83804-9f0b-4d97-8049-51c2c8c54665 | same1 | constant1 | super1
3a9cf8b5-8488-4278-a06a-fd75fa74e206 | same2 | constant2 | super2
db<>fiddle here
我想将 table2
中的多行插入到 table1
中。问题是我在 table2
和 table1
中有一个同名字段,如果该字段中已经有具有相同值的记录,我不想插入数据。现在我有这样的东西:
insert into table1 (id, sameField, constantField, superFied)
select gen_random_uuid(), "sameField", 'constant', "anotherField"
from table2;
我想我需要做这样的事情:
insert into table1 (id, sameField, constantField, superFied)
select gen_random_uuid(), "sameField", 'constant', "anotherField"
from table2
where not exists ... ?
如果我想要这个逻辑,我需要写什么而不是 ?
:当从 table2
? DBMS 是 Postgres。
您可以使用sub-query查看记录是否存在。您将需要定义应该唯一的列。
create table table2( id varchar(100), sameField varchar(25), constant varchar(25), superField varchar(25) ); insert into table2 values (gen_random_uuid(),'same1','constant1','super1'), (gen_random_uuid(),'same2','constant2','super2')
✓
2 行受影响
create table table1( id varchar(100), sameField varchar(25), constant varchar(25), superField varchar(25) ); insert into table1 values (gen_random_uuid(),'same1','constant1','super1');
✓
1 行受影响
insert into table1 (id, sameField, constant, superField) select uuid_in(md5(random()::text || clock_timestamp()::text)::cstring), t2.sameField, 'constant', t2.superField from table2 t2 where sameField not in (select sameField from table1)
1 行受影响
select * from table1; select * from table2;
id | samefield | constant | superfield :----------------------------------- | :-------- | :-------- | :--------- 4cf10b1c-7a3f-4323-9a16-cce681fcd6d8 | same1 | constant1 | super1 d8cf27a0-3f55-da50-c274-c4a76c697b84 | same2 | constant | super2 id | samefield | constant | superfield :----------------------------------- | :-------- | :-------- | :--------- c8a83804-9f0b-4d97-8049-51c2c8c54665 | same1 | constant1 | super1 3a9cf8b5-8488-4278-a06a-fd75fa74e206 | same2 | constant2 | super2
db<>fiddle here