简体   繁体   中英

How can i change this Oracle SQL to MySQL

The following Oracle SQL is

merge into table1 rs 
using table2 ch 
on (rs.id = ch.id) 
when matched then 
    update set rs.column = ch.column 
    where rs.column is not null

I want to change this SQL to MySQL. How can i do that?

That merge is only doing an update, not an insert.

So you can use an update statement for it

update table1 rs
inner join table2 ch on rs.id = ch.id
set rs.column = ch.column
where rs.column is not null

And if you would need an upsert, MySql has an insert on duplicate key update syntax

mySQL does not have a "MERGE" statement. However, there are 3 ways you can achieve this:
1. Using @valicu2000 's solution.
2. Using REPLACE syntax

REPLACE into table1 rs(`column`) values(select column from table2 ch) where table1.id=table2.id and table2.column is not null

3. Try a transactional update and insert(but, that's gonna be 2 queries)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM