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

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

Related

How to copy the last inserted row and paste it in other table in SQL?

I have 3 tables, using after insert trigger I am trying to take the last inserted row in table1 , then comparing the row parameters with table2 , and if it is not exists in table2 , then take the row and insert it in table3.
How can I build this trigger to work ?
In table1 I called the trigger " send_to_3"
table1
AFTER
INSERT
defenition :
I dont know how to write the code before the "IF"...
INSERT INTO 'table3'('ID','name','last_name') VALUES (?,?,?)
IF(new.data = (SELECT data FROM table2 WHERE table2.data <> new.data),1,0)
INSERT
IF(new.data = (SELECT data FROM table2 WHERE table2.data <> new.data ),1,0)
You can write this without an if:
insert into table3 (id, name, lastname)
select n.*
from (select new.id, new.name, new.lastname, new.data) n
where not exists (select 1
from table2 t2
where t2.data = n.data
);

MYSQL - Multiple value insert into table where not exists in another

I have PHP array(array has multiple values) and i want to add that data in table but before insert into table check for value is exist in another table, if value get in another table skip that value.
Example:
INSERT INTO table1('column1','column2','column3') VALUES
('val11','val11','val11'),
('val12','val12','val12')
WHERE NOT EXIST
(SELECT * FROM table2 WHERE table2.column1 = VALUES(column1) AND
table2.column2 = VALUES(column2) AND table2.column3 = VALUES(column3)
Am I on the right path or is there a better way of doing this?
Thanks
Miten
You need to use INSERT ... SELECT syntax to achieve this result. Because you have multiple rows you have to UNION the data rows into one derived table, and then check that the values in that table don't exist in table2:
INSERT INTO table1 (column1, column2, column3)
SELECT *
FROM (SELECT 'val11' AS column1, 'val12' AS column2, 'val13' AS column3
UNION
SELECT 'val21', 'val22', 'val23') v
WHERE NOT EXISTS (SELECT *
FROM table2 t2
WHERE t2.column1 = v.column1
AND t2.column2 = v.column2
AND t2.column3 = v.column3)
Demo on dbfiddle

MySQL Conditional insert to one table if a certain row does not exist on another table

Is it possible to do an insert query like this?
INSERT INTO `table_1`
VALUES ('val1','val2','val3')
WHERE (
SELECT COUNT(*)
FROM `table_2`
WHERE col1='somevalue'
)=0;
You can do this using insert . . . select:
INSERT INTO `table_1`(col1, col2, col3)
SELECT col1, col2, col3
FROM (SELECT 'val1' as col1,'val2' as col2, 'val3' as col3) t
WHERE NOT EXISTS (SELECT 1
FROM table_2 t2
WHERE t2.col1 = 'somevalue'
);
Notes:
This seems really strange. Limiting inserts into table_1 when no row exists in table_2 with other values.
NOT EXISTS is more efficient than using COUNT(*).
You should always list the columns when using INSERT.

Select data from other table during insert?

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

MYSQL, Copy selected fields from one table to another

In MySQL, How do I copy a FIELD with all RECORDS from TABLE1 to TABLE2 which corresponds to a primary key ie: EMPLOYEE no.?
If you mean you want to update one table's column using another table's column, then here are some options:
A join:
UPDATE table1 AS t1
INNER JOIN table2 AS t2 ON t1.EmpoyeeNo = t2.EmployeeNo
SET t1.SomeColumn = t2.SomeColumn
Alternatively it could be a left join:
UPDATE table1 AS t1
LEFT JOIN table2 AS t2 ON t1.EmpoyeeNo = t2.EmployeeNo
SET t1.SomeColumn = t2.SomeColumn
which would essentially empty (set to NULL) the values where no match occurred.
A subquery:
UPDATE table1
SET SomeColumn = (
SELECT SomeColumn
FROM table2
WHERE EmployeeNo = table1.EmployeeNo
)
This is equivalent to the left join solution in #1.
Note that in all cases it is assumed that a row in table1 can match no more than one row in table2.
Try this
INSERT INTO `table2` (`field_name2`) SELECT `field_name` FROM `table1`
The query for copy data from one table to another is:
INSERT INTO `table2` (`field1`, `field2`)
SELECT `field1`, `field2` FROM `table1`
If you want to copy only selected values, then use where clause in query
INSERT INTO `table2` (`field1`, `field2`)
SELECT `field1`, `field2` FROM `table1`
WHERE `field1` = condition
update
table1 t1
join table2 t2 on t2.field = t1.field
set
t1.field1 = t2.matchingfield
where
t1.whatever = t2.whatever
You can use this to copy all the records from table1 into table2 with a condition.
Insert into table2 select * from table1 where field1=condition
Suppose if the table structure is as follows.
TableA - Col1, Col2 ,Col3
TableB - Col1, Col2 ,Col3
There is no need to select all column of the table to transfer data from 1 table to another table in same databse.
You can copy (insert) the rows from TableA to TableB.
Code as follows -
Insert into TableB (Col1, Col2 ,Col3)
Select Col1, Col2 ,Col3 from TableA
You can also do this -
Insert into TableB (Col1, Col2, Col3)
Select * from TableA
Both codes work , you need to see your requirement.
Generic code -
INSERT INTO table2 (column1, column2, column3, ...)
SELECT column1, column2, column3, ...
FROM table1
WHERE condition;
You can add 'Where' condition if you need.
Thank you!!!
INSERT INTO table_1(column-1, column-2) SELECT column-1, column-2 FROM table_2;
Insert into Delivery (DeliveredDate, appid, DownloadSize, UploadSize) select Delivered, Appid, DownloadSize,UploadSize from Delivery_Summary;