如果 table 中不存在记录,则使用 sql 中不存在的记录插入
Insertion if the record doesn't exist in the table using not exists in sql
好吧,我只想插入 table 中不存在的数据,如果它存在,则无需执行任何操作
insert into colleges (Id, Name, CreatedOn) values("20", "ASIET", "2017-12-14
06:44:32") where not exists (select id from colleges where name = "ASIET")
我遇到错误
Error Code: 1064. You have an error in your SQL syntax; check the manual
that corresponds to your MySQL server version for the right syntax to use
near 'where not exists (select id from colleges where name = "ASIET")' at
line 1
提前感谢您的帮助
因此,如果您不想让行具有相同的 ID 和名称,那么您可以将它们都设为 table 'colleges' 的主键,然后使用插入忽略。
要使用主键:
ALTER TABLE colleges
ADD PRIMARY KEY (id, name);
然后像这样使用插入忽略:
INSERT IGNORE INTO colleges (Id, Name, CreatedOn)
VALUES ("20", "ASIET", "2017-12-14 06:44:32");
好吧,我只想插入 table 中不存在的数据,如果它存在,则无需执行任何操作
insert into colleges (Id, Name, CreatedOn) values("20", "ASIET", "2017-12-14
06:44:32") where not exists (select id from colleges where name = "ASIET")
我遇到错误
Error Code: 1064. You have an error in your SQL syntax; check the manual
that corresponds to your MySQL server version for the right syntax to use
near 'where not exists (select id from colleges where name = "ASIET")' at
line 1
提前感谢您的帮助
因此,如果您不想让行具有相同的 ID 和名称,那么您可以将它们都设为 table 'colleges' 的主键,然后使用插入忽略。
要使用主键:
ALTER TABLE colleges
ADD PRIMARY KEY (id, name);
然后像这样使用插入忽略:
INSERT IGNORE INTO colleges (Id, Name, CreatedOn)
VALUES ("20", "ASIET", "2017-12-14 06:44:32");