MySQL 5.5 Community Server
MySQL 5.6 Community Server
Percona Configuration Wizard
XtraBackup 搭建主从复制
Great Sites on MySQL
Percona
MySQL Performance Blog
Severalnines
推荐管理工具
Sequel Pro
phpMyAdmin
推荐书目
MySQL Cookbook
MySQL 相关项目
MariaDB
Drizzle
参考文档
http://mysql-python.sourceforge.net/MySQLdb.html
reguse
V2EX  ›  MySQL

请教下两表更新(一对多)问题

  •  
  •   reguse · Oct 17, 2017 · 3430 views
    This topic created in 3132 days ago, the information mentioned may be changed or developed.
    表 A
    x y
    1 a
    2 b
    3 c

    表 B
    x y
    1.1 aa
    1.2 aaa
    2.1 bb
    2.2 bbb
    2.3 bbbb
    3.1 c

    结果
    x y
    1.1 a
    1.2 a
    1.3 a
    2.1 b
    2.2 b
    2.3 b
    3.1 c

    根据表 A 的 x 来更新表 B 的 y,也就是表 B 中 1.x 的都属于表 A 中的 1,然后将表 B 中的 y 更新成表 A 中对应的 y。
    Oracle 应该怎样写?下面的 sql 有问题
    update B set y=(select y from A where A.x=substr(B.x,0,1)) where substr(B.x,0,1) = A.x
    4 replies    2017-10-17 15:46:48 +08:00
    Hyeongo
        1
    Hyeongo  
       Oct 17, 2017
    update B
    set y=tba.y
    from B,A tba where tba.x=substr(B.x,0,1)
    这样试试
    reguse
        2
    reguse  
    OP
       Oct 17, 2017
    @Hyeongo 多谢,还是有点问题,不过在 where 后加个 select 解决了
    doublleft
        3
    doublleft  
       Oct 17, 2017
    问个问题,这种一对多查询,查 b.id 等于所有 a.id 的所有 a 表数据,是用 inner join 一次拉出来好,写子查询好,还是放在业务层先拉 a.id 然后在 b 库里 WHERE id in() 好?
    doublleft
        4
    doublleft  
       Oct 17, 2017
    接上

    @Hyeongo 请问有什么具体场景区分吗
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   3194 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 34ms · UTC 10:53 · PVG 18:53 · LAX 03:53 · JFK 06:53
    ♥ Do have faith in what you're doing.