MySQL 使用 WHERE 子句直接 INSERT INTO

MySQL direct INSERT INTO with WHERE clause(MySQL 使用 WHERE 子句直接 INSERT INTO)
本文介绍了MySQL 使用 WHERE 子句直接 INSERT INTO的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

第一个语法已经正确.

这篇关于MySQL 使用 WHERE 子句直接 INSERT INTO的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Hibernate reactive No Vert.x context active in aws rds(AWS RDS中的休眠反应性非Vert.x上下文处于活动状态)
Bulk insert with mysql2 and NodeJs throws 500(使用mysql2和NodeJS的大容量插入抛出500)
Flask + PyMySQL giving error no attribute #39;settimeout#39;(FlASK+PyMySQL给出错误,没有属性#39;setTimeout#39;)
auto_increment column for a group of rows?(一组行的AUTO_INCREMENT列?)
Sort by ID DESC(按ID代码排序)
SQL/MySQL: split a quantity value into multiple rows by date(SQL/MySQL:按日期将数量值拆分为多行)