I'd like to insert a row into table A, but only if another row in table B exists. For example something like this...
IF EXISTS (SELECT * FROM B WHERE id=1)
INSERT INTO A
(id, value1, value2)
VALUES (1, 'foo', 'bar')
However that doesn't work. What will?
INSERT INTO A (value1, value2, value3)
SELECT 'foo', 'bar', 'foo' FROM B WHERE ID = 1
One potential problem here is if your condition is met more than once it will insert as many rows so adjust your query to that, but it will do what you want, only insert if the conditions on the select are met.
Have a look at this piece of MySQL manual, it gives an example with SELECT, but maybe INSERT would also work in a similar fashion?
Related
Here's what I am trying to do..
INSERT IGNORE into table1(`name`,`phone`,`address`,`province`,`col1`, `col2`)
VALUES ((SELECT `name`,`phone`,`address`,`province` FROM table2
WHERE `country`= 'Canada'),'val1', 'val2');
Select statement basically returns multiple queries. How to handle scenario using a combo of values and select statement returning, multiple queries.
Thanks
Proper syntax:
INSERT IGNORE into table1(`name`,`phone`,`address`,`province`,`col1`, `col2`)
Select `name`,`phone`,`address`,`province`,'val1','val2'
from table2
where `country`= 'Canada'
INSERT INTO class
(name, description, personid)
Select name, description, 12 from Class where PersonID = 3;
Select * from Class
Select * from Person
Why is the values words is missing from above statement? I thought it should be like this insert into tableA('name') values('select name from tableB') ?
INSERT INTO my_table VALUES ()
Insert data one Table to another table
OR
Not Using Value keyword
Insert into Table2 (Name , Address , Mobile) Select Column 1, Column 2 , Column 3 From Table1
There are different techniques of INSERT, the code above is inserting values from the table itself and change only the personid to 12, he use select so that he can copy the data aside from hardcoded personid . that's why you didn't see the VALUES keyword , but that's true.. the basic insert statement we learn from school is INSERT INTO TableName (Col1, Col2... etc) VALUES (Value1, Value2... etc) , INSERTION of data depends on the requirements that you are working on.
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 :)
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.
you can use UPDATE command.
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.
The syntax is as follows:
UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;
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.
Example of how to perform a INSERT INTO SELECT with a WHERE clause.
INSERT INTO #test2 (id) SELECT id FROM #test1 WHERE id > 2
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;
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,...)
I'm trying to do a select from one table and insert the return values into another table with a common value representing the search parameter using the insert/select function. For simplicities sake instead of...
INSERT INTO tbl_name (a,b) VALUES (1,1,1), (4,5,6);
I want to do something like this....
INSERT INTO tbl_name (a,b) VALUES (1), (4,5,6);
Except where the second column b has several hundred values and 'a' is the common value. I've tried using SET for 'a' but either this can't be done or I can't get the syntax correct. The reason I'm doing this is to avoid building up an insert function in PHP. Here is the best I have....
INSERT INTO tbl_name
(a,b)
SET a = '1'
SELECT c
FROM tbl_name2
WHERE `d` LIKE '%word%'
I might be missing the point, but doing:
INSERT INTO tbl_name (a,b) (SELECT 1,c FROM tbl_name2 WHERE d like '%word%';);
will do the job.
OK I FIGURED IT OUT, already typed all of this, might as well post the answer for everyone.
INSERT INTO tbl_name
(a,b)
SELECT "2", c
FROM tbl_name2
WHERE d LIKE '%word%'
I have trawled through tens of forums now to find MySql syntax to insert a row if there is no other row in the table that contains a give value.
I know it has to be simple but so far have found nothing that does what I need...
My requirement is simply:
if not exists (select * from table1 where int_value2 = 123) then
insert into table1 (value1, int_value2, value3)
values ('a', 1, 'a');
I apologise for how simple I know this is going to be but thanks in advance for any help you can offer.
Define a UNIQUE constraint if it not already exists:
ALTER TABLE table1 ADD UNIQUE(int_value2);
INSERT IGNORE INTO table1 (value1, int_value2, value3) VALUES ('a', 1, 'a');
Note the 'IGNORE` bit.
If you need 'fresh' data in value1/value3, you could look at ON DUPLICATE KEY UPDATE.
INSERT INTO table1 (value1, int_value2, value3)
SELECT 'a', 1, 'a' FROM DUAL
WHERE NOT EXISTS(SELECT * FROM table1 WHERE int_value2=123);