MYSQL, Copy selected fields from one table to another - mysql

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;

Related

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

MERGE statement in SQL

Can we use MERGE statement between 2 tables with different columns?
I need to update few columns in target table T1 from source table T2 based on one condition(where T2.Song_code=T1.Song_code).
But t1 has some columns which are not available in Source table. So did not exactly get how it could be used to see if the rows match.
Can someone please explain?
SQL
MERGE INTO t1 AS target
USING t2 AS source
ON target.Song_code=source.Song_code
WHEN MATCHED THEN
UPDATE SET target.Song_name = source.Song_name -- columns to update
WHEN NOT MATCHED BY TARGET THEN
INSERT VALUES (source.Song_code,source.Song_name); -- leave empty string (' ',' ') for Column with no value
You can try like this :
INSERT INTO T2 (col1, col2, col3, col4)
SELECT t1.col1, t1.col2, t1.col3, t1.col4
FROM t1
WHERE T2.Song_code=T1.Song_code
Use JOIN with UPDATE statement
UPDATE TABLE1 T1
JOIN TABLE2 T2
ON T2.Song_code=T1.Song_code
SET TABLE1.col1 = TABLE2.col1,
TABLE1.col2 = TABLE2.col2

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.

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

Copy rows from one table to another, ignoring duplicates

Im trying to copy row from table to another using 2 coluom only as the tow table schema is not identical ,
am getting this error
Operand should contain 1 column(s)
Any tips whats wrong with my statement ?
Insert table1 ( screenname,list_id )
Select screenname,list_id
From table2 As T1
Where Not Exists (
Select 1
From table1 As T2
Where
(T2.screenname = T1.screenname,T2.list_id = T1.list_id)
)
try to change where condition from (T2.screenname = T1.screenname,T2.list_id = T1.list_id) to (T2.screenname = T1.screenname AND T2.list_id = T1.list_id)
(note AND keyword instead of comma)
Did you try INSERT INTO...ON DUPLICATE KEY syntax?
See MySQL manual here
You can create a unique index in table1 on the columns screenname and list_id
Then use the following statement
Insert ignore into table1 ( screenname,list_id )
Select screenname,list_id
From table2 As T1
Also try this query -
INSERT INTO table1 (screenname, list_id)
SELECT screenname, list_id FROM table2 t2
LEFT JOIN table1 t1
ON t1.screenname = t2.screenname AND t1.list_id = t2.list_id
WHERE
t1.screenname IS NULL AND t1.list_id IS NULL;
Use simple INSERT IGNORE
INSERT table1 (screenname, list_id) SELECT screenname, list_id FROM table2