mysql联表更新

 时间:2024-10-15 06:57:10

1、假定有两张表,一劢实醭萱张用户表user,另一张用户评论表userComment。两张表联合查询,更新其中一张表,sql写法如下:update user a, userComment bset b.userName= a.userName, b.userPic=a.userPicwhere a.userId=b.userIdand a.userStatus=0上面的sql将user表中userName和userPic字段属性同步到userComment表中,条件是userId相同。表a和表b不是原始的基本表,如果是查询的结果集也能执行。

2、两张表联合查询,更新其中一张表,也能用inner join 连接。update user ainner join userComment b ona.userId=b.userIdset b.userName= a.userName, b.userPic=a.userPicwherea.userStatus=0注意也可以用外连接left join查询更新。

3、上面的写法都是两张表关联,更新其中一张,其实也能两张都更新,写法基本相同。update user a, userComment bset b.userName= a.userName, b.userPic=a.userPic,b.commentCount=a.commentCountwhere a.userId=b.userIdand a.userStatus=0当然上面的sql只是语法示例,没有什么具体的业务含义。

  • ff14刷马副本怎么开
  • 黎明前线rpg攻略
  • 破晓传说sp怎么刷
  • 艾尔登法环职业技能怎么学
  • 《永恒之柱2:死亡之火》旧城神庙
  • 热门搜索
    羸弱怎么读 雪铁龙c6怎么样 一楼潮湿怎么办 通心粉怎么做 打印机已暂停怎么办 nervous怎么读 八马茶业怎么样 乌龟怎么养放多少水 尿液混浊是怎么回事 怎么取消页眉页脚