MySQL 使用 WHERE 子句直接 INSERT INTO

2022-01-09 00:00:00 where-clause insert mysql

我尝试用谷歌搜索这个问题,但只找到了如何使用两个表来解决问题,如下所示,

I tried googling for this issue but only find how to do it using two tables, as follows,

INSERT INTO tbl_member
SELECT Field1,Field2,Field3,... 
FROM temp_table
WHERE NOT EXISTS(SELECT * 
         FROM tbl_member 
         WHERE (temp_table.Field1=tbl_member.Field1 and
               temp_table.Field2=tbl_member.Field2...etc.)
        )

这适用于一种情况,但现在我的兴趣是直接从程序本身上传数据,而不使用两个表.我想要的是上传不在表格中的数据.我脑子里的sql是这样的,

This worked for one scenario,But now my interest is to upload data directly from the program itself without using two tables. What i want is to upload the data which is not in the table. The sql i had in my head was like the following,

INSERT INTO tbl_member (SensorIdValue, DataTimeValue, DataInValue, IncompleteValue, SpiValue, InfoValue)
VALUES ('Sensor.org', '20121017150103', 'eth0','','','')
WHERE (SensorIdValue != 'Sensor.org'AND DataTimeValue != '20121017150103'AND DataInValue != 'eth0'AND IncompleteValue != ''AND SpiValue != ''AND InfoValue != '');

但这是错误的..请我知道正确的做法,非常感谢:)

But it's wrong.. may i know the proper way of doing it please, Thank you very much :)

推荐答案

INSERT 语法不能有 WHERE 子句.只有在使用 INSERT INTO...SELECT 语句时,您才会发现 INSERTWHERE 子句.

INSERT syntax cannot have WHERE clause. The only time you will find INSERT has WHERE clause is when you are using INSERT INTO...SELECT statement.

第一个语法已经正确.

相关文章