【问题标题】:Fill data from another table sql从另一个表sql填充数据
【发布时间】:2020-06-07 12:46:16
【问题描述】:

我想知道如何实现这一点: 给定表 A 和 B

表 A

id | name
1  | Alex
2  | Bob
3  | Cindy

表 B

scene_id | name
1        | Alex
2        | Alex
3        | Cindy
4        | Bob
5        | Cindy
6        | Alex

通过添加一列更新表 B,使其如下所示: 表 B'

scene_id | name  | id
1        | Alex  | 1
2        | Alex  | 1
3        | Cindy | 3
4        | Bob   | 2
5        | Cindy | 3
6        | Alex  | 1

我可以知道如何用 SQL 编写代码吗?

【问题讨论】:

    标签: mysql sql select sql-update create-table


    【解决方案1】:

    考虑:

    alter table tableb add column id int;
    
    update tableb b
    inner join tablea a on a.name = b.name
    set b.id = a.id
    

    Demo on DB Fiddle

    场景 ID |姓名 | ID --------: | :---- | -: 1 |亚历克斯 | 1 2 |亚历克斯 | 1 3 |辛迪 | 3 4 |鲍勃 | 2 5 |辛迪 | 3 6 |亚历克斯 | 1

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-01-12
      • 1970-01-01
      • 1970-01-01
      • 2023-04-07
      • 2017-03-02
      • 1970-01-01
      • 1970-01-01
      • 2023-01-14
      相关资源
      最近更新 更多