MySQL 以 NOT IN 条件插入并加入 table
MySQL insert and join table with NOT IN condition
我想从 table 中插入数据,但我不想在我的目的地 table.
中再次导入相同的日期数据
例如-我的源和目的地都有日期作为一列table,我不想在目的地table中使用相同的日期数据。 table 中的日期列相同,table 中的列数也相同。
我怎样才能做到这一点?
筛选那些日子并插入
INSERT INTO destination_table
select a.* from source a
where a.date not in ( select date from destination_table)
最好的方法是使用 NOT EXISTS
如下:
INSERT INTO destination_table
select *
from source_table s
where not exists
(select 1 from destination_table d where d.date = s.date)
您可以按如下方式创建目的地 table:
CREATE TABLE Destination (
ID int NOT NULL,
Property1 PropertyType,
Property2 PropertyType,
etc.....
UniqueDate DATE,
UNIQUE (UniqueDate)
);
记得用您自己的属性和类型替换 PropertyX 和 PropertyType,并重命名 UniqueDate
此代码将在您的日期列上设置 UNIQUE 约束。
我想从 table 中插入数据,但我不想在我的目的地 table.
中再次导入相同的日期数据例如-我的源和目的地都有日期作为一列table,我不想在目的地table中使用相同的日期数据。 table 中的日期列相同,table 中的列数也相同。
我怎样才能做到这一点?
筛选那些日子并插入
INSERT INTO destination_table
select a.* from source a
where a.date not in ( select date from destination_table)
最好的方法是使用 NOT EXISTS
如下:
INSERT INTO destination_table
select *
from source_table s
where not exists
(select 1 from destination_table d where d.date = s.date)
您可以按如下方式创建目的地 table:
CREATE TABLE Destination (
ID int NOT NULL,
Property1 PropertyType,
Property2 PropertyType,
etc.....
UniqueDate DATE,
UNIQUE (UniqueDate)
);
记得用您自己的属性和类型替换 PropertyX 和 PropertyType,并重命名 UniqueDate
此代码将在您的日期列上设置 UNIQUE 约束。