如果条目不存在,则插入表中

时间:2013-09-03 08:04:04

标签: liquibase

所有我想要创建一个更改集,它插入一个迭代表的所有条目,并由primaryKey查看另一个表。如果在此表中没有该primaryKey的条目,则应在2个不同的表中执行2次插入。有谁知道我怎么解决这个问题?

致谢并感谢您的帮助

1 个答案:

答案 0 :(得分:2)

table1 (data to use for check)  I'm assuming you want to pull some data from here, if not replace table1.col3 below with the data you want.
table2 (data to check against)  Assumes your FK column is table1_id
table3 (table to insert to)
table4 (table to insert to)

我会使用2个更改集(假设表1和表2中的数据在运行时不会改变)

<changeSet>
  <sql>insert into table3 (col1, col2) (select table1.col3, 'val1' from table1 where table1.id not in(select table2.table1_id from table2))</sql>
</changeSet>
<changeSet>
  <sql>insert into table4 (col1, col2) (select table1.col3, 'val1' from table1 where table1.id not in(select table2.table1_id from table2))</sql>
</changeSet>