【问题标题】:How to merge two tables together with same number of rows corresponding to same id numbers?如何将两个表与对应相同id号的相同行数合并在一起?
【发布时间】:2022-01-24 01:29:54
【问题描述】:

MySQL:如何将两个表合并在一起,相同的行数对应相同的 ID 号?有人可以帮我写一个查询。我是这样写的:

INSERT INTO table1.code
SELECT code FROM table2
WHERE table1.id = table2.id

但是,我收到 mysql 错误:#1054 - Unknown column 'table1.id' in 'where clause'

表 1

id name code
1 abc
2 def
3 ghi

表 2

id code
1 12
2 Ab
3 D2

必需的 MYSQL 数据库表 1

id name code
1 abc 12
2 def Ab
3 ghi D2

【问题讨论】:

标签: mysql sql


【解决方案1】:

看起来你只需要加入你的两个表并更新table1

update t1
join t2 on t1.id = t2.id
set t1.code = t2.code

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-10-13
    • 2014-12-09
    • 1970-01-01
    • 2016-11-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多