Select data from other table during insert? - mysql

I am inserting data into a database that looks like this:
(1, 'blue'), (2,'large'), (3, 'round')
The numbers there correspond to ID's from another table. that looks like: id | value
When inserting this data I want to insert the actual value that the number corresponds to, not the id.
Is there any query to do this? or do I need match the values before sending it to the database?
While I know it won't work, I am hoping there is something like:
insert into table2 (table1.value[id=1], 'blue'), (table1.value[id=2],'large'), (table1.value[id=3], 'round') join table1
I imagine I could use:
insert into table2
((select value from table1 where id=1), 'blue'),
((select value from table1 where id=2),'large'),
((select value from table1 where id=3), 'round')
But with say, 40 different attributes that would make 41 queries!

First virtually make up a table with the values you want to insert (id,value), then join the derived table to table1 and INSERT the result into table2.
insert into table2
select t.value, madeup.other
from (select 1 id, 'blue' other union all
select 2, 'large' union all
select 3, 'round') madeup
join table1 t on t.id = madeup.id;

You could use a temporary table to map id to value. I don't really speak MySQL, but something like this:
create table #mapping (id int, description varchar)
insert into #mapping values (1, 'blue')
insert into #mapping values (2, 'large')
insert into #mapping values (3, 'round')
insert into table2
select table1.value, #mapping.description
from #mapping
join table1 on table1.id = #mapping.id
drop table #mapping

Related

How to make a join only on selected values of a column on another table in sql

Hello I have a main table table1 that has n columns with a column name called R_Id this column is a primary key (Id) in another table table2. I want to make a join such that I want to retain all the values and columns of table1 but filter the table1 where the R_Id is 13, 3 and 4.
Simply, I can try this i.e.
select * from table1
where table1.R_Id in (13, 3, 4)
but this will filter the whole table1 based on these three values. I want to retain the table1 and all its value and filter only this particular column. I tried something like this but it doesn't work
select * from table1 t1
left join table2 t2 on t1.R_Id = t2.Id
where t1.R_Id in (13, 3, 4)
But this unfortunately doesn't work.
You must set the condition in the ON clause, so that all rows of table1 are returned but only the rows with t1.R_Id IN (13, 3, 4) are joined to table2:
SELECT *
FROM table1 t1 LEFT JOIN table2 t2
ON t1.R_Id = t2.Id AND t1.R_Id IN (13, 3, 4)

How to build a large column of values?

What's the best way to dynamically build a column of values?
My PHP app will have the ID fields of a large number of records (<=30k). I need to update these records and I'm wondering if I'm overlooking an easy to do this. What I've considered so far is:
(1) Use a derived table built using UNION ALL.
UPDATE t1
INNER JOIN ( SELECT 1 id UNION ALL
SELECT 2 id UNION ALL
SELECT 3 id UNION ALL
SELECT 4 ) t2 ON t1.id = t2.id
SET t1.status = 'A'
In testing, I've exhausted my memory limit by too many UNION ALLs.
(2) Use a temporary table. The INSERT statement can be built by imploding my array of ID fields.
CREATE TEMPORARY TABLE temp (id INT)
INSERT INTO temp (id) VALUES (1), (2), (3), (4)
UPDATE t1
INNER JOIN temp ON t1.id = temp.id
SET t1.status = 'A'
Is there some other way? Thanks!
That's pretty much it if you are using MySQL.
Some engines support the VALUES clause as a table value constructor(like SQL Server), so you could skip the temporary table step.
select * from (VALUES (1), (2), (3)) q(i);
Similar question here: Table Values() Constructor for Updating Multiple Rows
Looks like you are stuck with a temp table or UNION if you have to UPDATE.
If you can try this (but it will create rows that aren't there)...
INSERT INTO t1 (id, status) VALUES (1, 'A'), (2, 'A'), (3, 'A')
ON DUPLICATE KEY UPDATE status=VALUES(status)
Example SQLFiddle

Insert into Table2 only if values being inserted are found in Table1

I have two tables Table1 and Table2.
I use an insert on Table2 like below:
insert into table2
(colOne, colTwo, colThree) //and many more cols
values
(1, norman, US) //and many more values that come from a form and not table1
I'd like the insert to succeed only if values (1,norman, US) are present in Table1. Values (1,Norman,US) come from a form. How can I do something like this.
At present I used 2 steps to do this. One to check if the values exist, two - the insert
You may use an INSERT INTO... SELECT... WHERE
Something liket that
insert into table2 (col1, col2, col3)
select (1, 'norman', 'US') from Table1 t1 -- where 1, 'norman', 'US' are your variables from a Form
where t1.id=1 and t1.name = 'norman' and t1.country = 'US' -- same
little SqlFiddle demo for "select whatever I want".
You can use this method :
INSERT INTO table2
(colOne, colTwo, colThree)
SELECT colOne, colTwo, colThree
FROM
(SELECT 1 AS colOne,'norman' AS colTwo,'US' AS colThree
UNION
SELECT 2,'sabt','US'
UNION
SELECT 3,'ebi','US'
)p
WHERE
EXISTS (
SELECT 1 FROM table1
WHERE table1.colOne = p.colOne AND
table1.colTwo =p.colTwo AND
table1.colThree =p.colThree
)
Good luck.
Something like this is normally done with triggers. Register an after insert trigger for table1 and do an insert for table2.
http://technet.microsoft.com/de-de/library/ms189799.aspx

Multiple inserts/updates without duplication in mysql

I want many values to be simultaneously inserted in my table having only 2 columns and if those values already exists then it has to be updated.. Though duplication for 1 column is possible but not for the second column.. I can easily do it with the following query.. But the problem is here only one row can only be considered... There are no primary keys.. PLZ HELP
INSERT INTO `table` (value1, value2)
SELECT 'stuff for value1', 'stuff for value2' FROM `table`
WHERE NOT EXISTS (SELECT * FROM `table`
WHERE value1='stuff for value1' AND value2='stuff for value2')
LIMIT 1
Try this
Insert into table name.............
on duplicate key update set column1=......
Alternative way ::
Step1 : Create a temp_table with same structure of that of table1
Step 2:
INsert into temp_Table
(SELECT * from table1 t1 left join table2 t2 on (t1.value1=t2.value1 and t1.value2=t2.value2)
where t2.value1 is null and t2.value2 is null);
Step3:
INsert into table Select * from temp_table

Help with INSERT INTO..SELECT

I'm inserting a large number of rows into Table_A. Table_A includes a B_ID column which points to Table_B.B_ID.
Table B has just two columns: Table_B.B_ID (the primary key) and Table_B.Name.
I know the value for every Table_A field I'm inserting except B_ID. I only know the corresponding Table_B.Name. So how can I insert multiple rows into Table_A?
Here's a pseudocode version of what I want to do:
REPLACE INTO Table_A (Table_A.A_ID, Table_A.Field, Table_A.B_ID) VALUES
(1, 'foo', [SELECT B_ID FROM Table_B WHERE Table_B.Name = 'A'),
(2, 'bar', [SELECT B_ID FROM Table_B WHERE Table_B.Name = 'B'),...etc
I've had to do things like this when deploying scripts to a production environment where Ids differed in environments. Otherwise it's probably easier to type out the ID's
REPLACE INTO table_a (table_a.a_id, table_a.field, table_a.b_id)
SELECT 1, 'foo', b_id, FROM table_b WHERE name = 'A'
UNION ALL SELECT 2, 'bar', b_id, FROM table_b WHERE name = 'B'
If the values:
(1, 'foo', 'A'),
(2, 'bar', 'B'),
come from a (SELECT ...)
you can use this:
INSERT INTO Table_A
( A_ID, Fld, B_ID)
SELECT Data.A_ID
, Data.Field
, Table_B.B_ID
FROM (SELECT ...) As Data
JOIN Table_B
ON Table_B.Name = Data.Name
If not, you can insert them into a temporary table and then use the above, replacing (SELECT ...) with TemporaryTable.
CREATE TABLE HelpTable
( A_ID int
, Fld varchar(200)
, Name varchar(200)
) ;
INSERT INTO HelpTable
VALUES
(1, 'foo', 'A'),
(2, 'bar', 'B'), etc...
;
INSERT INTO Table_A
( A_ID, Field, B_ID)
SELECT HelpTable.A_ID
, HelpTable.Fld
, Table_B.B_ID
FROM HelpTable
JOIN Table_B
ON Table_B.Name = HelpTable.Name
;
DROP TABLE HelpTable ;