简体   繁体   English

MySQL 直接 INSERT INTO 带 WHERE 子句

[英]MySQL direct INSERT INTO with WHERE clause

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.)
        )

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,我脑海中的 sql 如下所示,

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 syntax cannot have WHERE clause. INSERT语法不能有WHERE子句。 The only time you will find INSERT has WHERE clause is when you are using INSERT INTO...SELECT statement.只有在使用INSERT INTO...SELECT语句时才会发现INSERT具有WHERE子句。

The first syntax is already correct.第一个语法已经正确。

您可以使用 UPDATE 命令。

UPDATE table_name SET name=@name, email=@email, phone=@phone WHERE client_id=@client_id

INSERT syntax cannot have WHERE but you can use UPDATE . INSERT语法不能有WHERE但你可以使用UPDATE

The syntax is as follows:语法如下:

UPDATE table_name

SET column1 = value1, column2 = value2, ...

WHERE condition;

如何使用 WHERE 子句执行 INSERT INTO SELECT 的示例。

INSERT INTO #test2 (id) SELECT id FROM #test1 WHERE id > 2

If I understand the goal is to insert a new record to a table but if the data is already on the table: skip it!如果我理解目标是向表中插入一条新记录,但如果数据已经在表中:跳过它! Here is my answer:这是我的回答:

INSERT INTO tbl_member 
(Field1,Field2,Field3,...) 
SELECT a.Field1,a.Field2,a.Field3,... 
FROM (SELECT Field1 = [NewValueField1], Field2 = [NewValueField2], Field3 = [NewValueField3], ...) AS a 
LEFT JOIN tbl_member AS b 
ON a.Field1 = b.Field1 
WHERE b.Field1 IS NULL

The record to be inserted is in the new value fields.要插入的记录位于新值字段中。

merge into table2 chg using table1 src on src.id = chg.id when not matched then insert (chg.id, chg.desc) values (src.id, src.desc) when matched then update set chg.desc = src.desc;当不匹配时使用 table1 src on src.id = chg.id 合并到 table2 chg 匹配时插入 (chg.id, chg.desc) 值 (src.id, src.desc) 然后更新设置 chg.desc = src。描述;

The INSERT INTO Statement INSERT INTO 语句
The INSERT INTO statement is used to insert a new row in a table. INSERT INTO 语句用于在表中插入新行。
SQL INSERT INTO Syntax SQL INSERT INTO 语法

It is possible to write the INSERT INTO statement in two forms.可以用两种形式编写 INSERT INTO 语句。

The first form doesn't specify the column names where the data will be inserted, only their values:第一种形式不指定将插入数据的列名,只指定它们的值:

INSERT INTO table_name
VALUES (value1, value2, value3,...)

The second form specifies both the column names and the values to be inserted:第二种形式指定要插入的列名和值:

INSERT INTO table_name (column1, column2, column3,...)
VALUES (value1, value2, value3,...)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM