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.
Related
I would like to insert new values into a table where one of the values is being selected from another table with a condition, and the other value is a constant (hardcoded)
this command gives a syntax error
INSERT INTO table1 (itemId, reservedId) VALUES (SELECT id FROM table2 WHERE condition, 213) ;
error message : syntaxe error at line 2
The syntax is INSERT . . . SELECT:
INSERT INTO table1 (itemId, reservedId)
SELECT id, 123
FROM table2
WHERE condition ;
No VALUES is needed.
You can't mix values and select statements like this, as you've seen. You could, however, select the literal values you want to insert from the same table (note that you should not have a values clause - it's replaced by the select statement):
INSERT INTO table1 (itemId, reservedId)
SELECT id, 213 FROM table2 WHERE condition;
You can either use
insert into yourtable(...) values(...)[, (...)...]
or
insert into yourtable(...) select ...
Your mistake was that you mixed up the two syntaxes. You will not need values(...) here wrapped around your select:
INSERT INTO table1 (itemId, reservedId)
SELECT id, 213
FROM table2
WHERE condition;
Notice that 213 is a value for reservedId and has nothing to do in the where clause.
I would like to insert new values into a table where one of the values is being selected from another table with a condition, and the other value is a constant (hardcoded)
this command gives a syntax error
INSERT INTO table1 (itemId, reservedId) VALUES (SELECT id FROM table2 WHERE condition, 213) ;
error message : syntaxe error at line 2
The syntax is INSERT . . . SELECT:
INSERT INTO table1 (itemId, reservedId)
SELECT id, 123
FROM table2
WHERE condition ;
No VALUES is needed.
You can't mix values and select statements like this, as you've seen. You could, however, select the literal values you want to insert from the same table (note that you should not have a values clause - it's replaced by the select statement):
INSERT INTO table1 (itemId, reservedId)
SELECT id, 213 FROM table2 WHERE condition;
You can either use
insert into yourtable(...) values(...)[, (...)...]
or
insert into yourtable(...) select ...
Your mistake was that you mixed up the two syntaxes. You will not need values(...) here wrapped around your select:
INSERT INTO table1 (itemId, reservedId)
SELECT id, 213
FROM table2
WHERE condition;
Notice that 213 is a value for reservedId and has nothing to do in the where clause.
INSERT INTO my_table (field_1, field_2)
SELECT val_1, val_2
FROM my_table
WHERE NOT EXISTS (SELECT field_1
FROM my_table
WHERE field_2 = val_2)
LIMIT 1
I can not use unique index on field_2 field.
I'm trying to insert if not exists a tuple with field2 = val_2.
Without the "where" clause, this insert.
With the "where" clause EVEN WHEN EMPTY TABLE, it won't insert.
Any help on that?
I guess val_1 and val_2 are not columns of the table, right?
They are values that you want to insert in the table.
So drop:
FROM my_table
and use:
INSERT INTO my_table (field_1, field_2)
SELECT val_1, val_2
WHERE NOT EXISTS (
SELECT field_1
FROM my_table
WHERE field2 = val_2
)
Solution is that check if key exist:
INSERT INTO my_table (field_1, field_2) ON DUPLICATE KEY INSERT IGNORE.
this question maybe useful.
First create a unique index on the column or columns that you don't want duplicated. I cannot tell if it is on both or just field_2:
create unique index unq_my_table_field_2 on my_table(field_2);
Then ignore the error using on duplicate key update:
INSERT INTO my_table (field_1, field_2)
VALUES (val_1, val_2)
ON DUPLICATE KEY UPDATE field_2 = VALUES(val_2); -- this is a no-op, because the value is already the same
The fact the your code doesn't work on an empty table has nothing to do with the WHERE clause but with using from my_table in the from clause. If there are no rows, then the query returns . . . no rows. No surprise there that nothing gets inserted.
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 have two tables:
vote('id', 'question_id', 'ip_id')
and
ip('id','ip_addr')
I want to do something like this:
INSERT INTO SELECT `vote`.`question_id`, `ip`.`ip_addr`
FROM `vote`
LEFT JOIN `ip`
ON `vote`.`ip_id` = `ip`.`id` VALUES '2','127.0.0.1'
the above code is not working, any idea?
The syntax of INSERT INTO SELECT is like this:
INSERT INTO Table2
(Column1, Column3)
SELECT Column1, Column3
FROM Table1
So your query should be like this:
INSERT INTO <TableName> (`question_id`, `ip_addr`)
SELECT `vote`.`question_id`, `ip`.`ip_addr`
FROM `vote`
LEFT JOIN `ip`
ON `vote`.`ip_id` = `ip`.`id`
You can insert rows into an existing table using one of the following methods:
INSERT INTO vote (id, question_id, ip_id)
VALUES (....)
OR
INSERT INTO vote (id, question_id, ip_id)
SELECT id, question_id, ip_id
FROM
<some existing tables, optionally using joins, where clauses etc., to select the data to insert>
In your case, you seem to be trying to insert record in to the IP table as well as the VOTE table at the same time and that is not possible.
You should first insert a record in the IP table using the first syntax above and then insert into VOTE table using the second option.