MERGE statement in SQL - mysql

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

Related

Is it possible to shorten a SQL query?

I have one a SQL query.
INSERT INTO `t1`(col1, col2)
SELECT * FROM `t2`
WHERE NOT EXISTS (
SELECT*FROM `t1`
WHERE (
t1.col1 = t2.col1
AND
t1.col2 = t2.col2 )
);
This query compares two existing tables "t1" and "t2".
Inserts data from table “t2” into table “t1” without any duplicate records between the two tables.
How can I truncate this SQL query? Can it be written in another way? Easier?
Yes, correlated suqbuery could be rewritten as LEFT JOIN:
INSERT INTO t1 -- I suggest to explicitly list all columns
SELECT t2.*
FROM t2
LEFT JOIN t1 -- USING (col1, col2) -- instead of ON clause
ON t1.col1 = t2.col1
AND t1.col2 = t2.col2
WHERE t1.col1 IS NULL;
Related: Blind insert
If (col1,col2) is a unique key in t1, you can just
insert ignore into t1 select * from t2;

How can I use multiple columns of SELECT into UPDATE statement?

I have a query like this:
UPDATE t1
SET t1.col1 = ( SELECT col1 FROM t2 WHERE <some_complex_conditions> ),
t1.col2 = ( SELECT col2 FROM t2 WHERE <some_complex_conditions> )
WHERE id = :id;
As you see, I have to execute the same query twice, every time for one column. Also as I've mentioned, that SELECT query has some complex conditions which need lots of processing. Now I want to know, how can I handle the UPDATE statement to get the update those two columns by single SELECT statement?
Something like this:
SELECT col1, col2 FROM t2 WHERE <some_complex_conditions>
In other word, how can I use this ^ into the UPDATE statement?
see multi table syntax in manual and stackoverflow
It's possible to update joined table. I think that it should be possible to join select statement, but I'm not sure, but I think example below shows equivalent query:
UPDATE t1
LEFT JOIN t2
-- joining condition, there can be the part <some complex condition>
ON t1.id = t2.ref_id AND t2.col3 = 'whatever you want'
SET t1.col1 = t2.col1, t1.col2 = t2.col2
--additional condition like WHERE t2.col1 IS NOT NULL

Mysql - updating and insert using select * using a target column

update table1 t1
inner join
table2 t2 on
t1.a = t2.a
set t1.b = t2.b,
t1.c = t2.c;
This code works to join 2 tables on column a. My problem is that I have about 500 columns which I want to update and am currently writing out each of the 500 columns in the code up to
t1.500 = t2.500;
This works, but it is slow and inefficient. Does anyone know how you can select * from table2 to update table1, keeping the join on t1.a = t2.a? All of the column names match exactly and am inserting all of the columns from table2. Was thinking of something like this below although I know that this is not correct. Thank you!
update table1 t1
inner join
table2 t2 on
t1.a = t2.a
set t1.* = t2.*;
I think there is no way to make update query with a wildcard in mysql. Don't know if it will properly fit to your problem, but you can try this workaround. :
DELETE FROM table2 WHERE id IN (<ids>);
Delete all the records from table2 that are satisfying given condition. And then insert the corresponding records from table1 to table2.
INSERT INTO table2
SELECT * FROM table1 WHERE id IN (<ids>);

Merging two SQL Server tables conditionally into a third table

Clearly, I am not a SQL guy, so I have to ask for help on the following rather simple task.
I have two SQL Server 2008 tables: t1 and t2 with many identical columns and a key column (entry_ID). T2 has rows that do not exist in t1 but should.
I want to merge those rows from t2 that do not exist in t1 but I also do not want any rows from t2 that already exist in t1. I would like the result set to fill a new t3.
I have looked at many solutions online but can't find the solution to the above scenario.
Thank you.
There are a number of ways to do it you could use UNION ALL or OUTER JOIN.
Assuming you are using Entry_ID to find identical records, and Entry_ID is unique within each table, here is a OUTER JOIN method:
This gets you your recordset: T1 and T2 merged:
SELECT
CASE
WHEN T1.Entry_ID IS NULL THEN 'T2'
WHEN T2.Entry_ID IS NULL THEN 'T1'
ELSE 'Both'
END SourceTable,
COALESCE(T1.Entry_ID,T2.Entry_ID) As Entry_ID,
COALESCE(T1.Col1, T2.Col1) As Col1,
COALESCE(T1.Col2, T2.Col2) As Col2,
COALESCE(T1.Col3, T2.Col3) As Col3,
COALESCE(T1.Col4, T2.Col4) As Col4
FROM T1 FULL OUTER JOIN T2
ON T1.Entry_DI = T2.Entry_ID
ORDER BY COALESCE(T1.Entry_DI,T2.Entry_ID)
This inserts it into T3:
INSERT INTO T3 (Entry_ID,Col1, COl2,Col3,Col4)
SELECT
COALESCE(T1.Entry_DI,T2.Entry_ID) As Entry_ID,
COALESCE(T1.Col1, T2.Col1) As Col1,
COALESCE(T1.Col2, T2.Col2) As Col2,
COALESCE(T1.Col3, T2.Col3) As Col3,
COALESCE(T1.Col4, T2.Col4) As Col4
FROM T1 FULL OUTER JOIN T2
ON T1.Entry_DI = T2.Entry_ID
Again you must note that Entry_ID needs to be unique within their tables, and it uses this to match between the tables.
Also note the columns from the select line up with the column list in the insert statement - the order of the columns in the physical table doesn't matter, the INSERT and SELECT just have to line up.

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;