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);
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'
I want to make an sql query which can insert into table1. This table references table2 with the table2_id foreign key.
my sql looks like this so far:
INSERT INTO table1 (table1_id, name, date, table2_id)
VALUES (1, "somename", "29.04.2014", (SELECT id FROM table 2 WHERE table2.name = "BOB") )
I also want to insert values into table2 if it cannot find the table2.name and then insert the key for this into table1.
Anyone knows how to do this?
I would suggest that you use insert . . . select rather than insert . . . values:
INSERT INTO hovedenhet (organisasjonsnummer, navn, stiftelsesdato, registreringsdatoEnhetsregisteret, organisasjonsform_id)
SELECT 813550202, 'SAMEIET SCHWEIGAARDSGATE 21-23', '10.01.2014', '29.04.2014', id
FROM organisasjonsformhovedenhet oh
WHERE oh.organisasjonsform = 'BOB';
Your original query will insert the row with a NULL value for the last column, if there is no match. This will not insert anything in that case.
I am trying to read value from a table them insert it into another table only if the staring that I am trying to insert does not already exist in the table.
I have tried to use the ON DUPLICATE KEY clause but I get a syntax issue that i am unable to fix.
this is my query
SELECT Field1 FROM RSF
INSERT INTO result_codes(result_code_title, created_by)
VALUES (Field1, '2')
ON DUPLICATE KEY UPDATE result_code_title = Field1;
The clause is INSERT INTO ... SELECT and not SELECT ... INSERT INTO. Therefore:
INSERT INTO result_codes( result_code_title, created_by )
SELECT Field1, '2'
FROM RSF
ON DUPLICATE KEY
UPDATE result_code_title = Field1;
I think that should work.
Since you only want to insert only if the string doesn't already exist; you should be better off using INSERT INGORE:
INSERT IGNORE INTO result_codes( result_code_title, created_by )
SELECT Field1, '2'
FROM RSF
Use INSERT IGNORE instead.
INSERT IGNORE INTO result_codes(result_code_title, created_by)
SELECT Field1, '2'
FROM RSF
It detects via a primary key or unique index, if a row already exists and doesn't attempt to insert if this is the case.
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'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?