MySQL使用WHERE子句直接INSERT INTO [英] MySQL direct INSERT INTO with WHERE clause

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

问题描述

我尝试使用Google搜索来解决此问题,但仅使用以下两个表来查找方法,

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具有WHERE子句的时间是在使用INSERT INTO...SELECT语句时.

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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆