mysql - 使用匹配 ID 将数据从表 1 插入和更新到表 2

标签 mysql

这是我的问题:下面有两个示例表,我尝试根据匹配 ID 从表一更新并插入到表二。但不工作。当不存在时添加到表 2

表1

   ID   DESC
1. 1    one
2. 2    two
3. 3    three
4. 4    four

表2

   ID   DESC
1. 1    ones
2. 2    
3. 3    three

INSERT INTO 
notif_test(ntfy_ttl,ntfy_desc,ntfy_typ,beg_dt,del_nbr,crt_ts,crt_nbr,
upd_ts,upd_nbr) select
ntfy_ttl,ntfy_desc,ntfy_typ,beg_dt,del_nbr,crt_ts,crt_nbr,upd_ts,upd_nbr
from notif_dump
ON DUPLICATE KEY UPDATE ntfy_desc = VALUES(ntfy_desc)

最佳答案

查看您的数据,您似乎正在寻找 DESC 为空的值的更新

update table1 a 
inner join table2 b
on a.id = b.id
set a.desc = b.desc
where a.desc is null  

关于mysql - 使用匹配 ID 将数据从表 1 插入和更新到表 2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47058655/

相关文章:

php - 我需要使用表中的员工 ID 从 mysql 数据库中获取数据

php - 在 php 中查询或更改列名?

mysql - 在嵌套SQL中如何对值进行求和

MySQL 5.0 报告 "concat does not exist"

mysql - 有时我得到 IoT Mysql 查询 - 子查询返回超过 1 行

php - 如何从 Laravel 获取数据库中列的平均值

mysql - 通过左连接表的 id 从表中选择行

MySQL 索引大表的性能

php - 在自动增量列中保留一组 id

mysql - Codeigniter + MySQL - 搜索包含字符串