MySQL direct INSERT INTO with WHERE clause
Asked Answered
K

8

16

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

Knute answered 18/10, 2012 at 6:38 Comment(3)
Based on the SQL you had in mind, it looks like you want to insert a row into a table if it doesn't already exist. If this is the case, please take a look at this question/answer. #1361840Understand
view this question. it might help. #2930878Laplace
yeaaa thats exaclty was my need il check on these..thank you very much for the replies :)Knute
M
28

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.

The first syntax is already correct.

Mcabee answered 18/10, 2012 at 6:40 Comment(2)
okey thank you, ill try optimizing the first query for my need :)Knute
yea how stupid of me just needed to call the same table in the select query and match it with the values i wanted.. :) thanks alot :)Knute
K
14

you can use UPDATE command.

UPDATE table_name SET name=@name, email=@email, phone=@phone WHERE client_id=@client_id
Kv answered 29/10, 2018 at 5:52 Comment(1)
simplest solution, and works in mysql.Gutty
M
6

INSERT syntax cannot have WHERE but you can use UPDATE.

The syntax is as follows:

UPDATE table_name

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

WHERE condition;
Mckay answered 14/12, 2020 at 3:32 Comment(0)
D
1

Example of how to perform a INSERT INTO SELECT with a WHERE clause.

INSERT INTO #test2 (id) SELECT id FROM #test1 WHERE id > 2
Donahoe answered 30/10, 2015 at 8:31 Comment(3)
It's self explained. This is an example to show that you can INSERT INTO with a WHERE clause. it's a really simple example where anyone should be able to grasp the concept.Donahoe
Down vote is not mine, I like to comment instead. As you see, others disagree. I found your post in low quality posts, so I was suggesting you improve it.Ondometer
Cool, well i am going to leave it up even with the down vote as i do believe it could help someone later on.Donahoe
S
1

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.

Splenitis answered 25/7, 2016 at 21:15 Comment(0)
A
0
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;
Ancon answered 19/12, 2022 at 17:40 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Merriment
A
0

Your second scenario is achievable using cursors, which are simple enough to use

Admittedly answered 19/6 at 5:49 Comment(0)
S
-3

The INSERT INTO Statement
The INSERT INTO statement is used to insert a new row in a table.
SQL INSERT INTO Syntax

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:

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,...)
Swoop answered 18/10, 2012 at 6:48 Comment(1)
thank you for the reply but i wanted to perform a conditional 'INSERT INTO' statement :)Knute

© 2022 - 2024 — McMap. All rights reserved.