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,
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 :)
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:
The record to be inserted is in the new value fields.
It is possible to write the INSERT INTO statement in two forms.
The first form doesn't specify the column names where the data will be inserted, only their values:
The second form specifies both the column names and the values to be inserted:
INSERT
syntax cannot haveWHERE
clause. The only time you will findINSERT
hasWHERE
clause is when you are usingINSERT INTO...SELECT
statement.The first syntax is already correct.
Example of how to perform a INSERT INTO SELECT with a WHERE clause.
you can use UPDATE command.