[不花钱站长]:从免费域名开始[0元] Oracle永久免费VPS[0元] VPS环境搭建免费脚本[0元] 秒变大盘挂载免费网盘[0元] 小白免费采集器1天500W[0元] CF免费DNS+CDN服务[0元]
[零基础网赚]:撸本站免费源码[0元] 网站强引免费蜘蛛法[0元] 图片视频文件免费存储[0元] 外链轻松发布免费友链[0元] 入坑免费教程学习源代码[0元] 网上百种+赚钱联盟推荐[?元]

↑↑VPS推荐网 WWW.VPS.COM.IN 稳定 便宜 免备案↑↑


您现在的位置是:首页 > 全球[VPS测评] >  永久免费VPS 香港VPS,国外永久免费VPS

mysql update 使用 select 的结果的方法

全球[VPS测评]来源:VPS推荐网点击:1527
【性价之王】【线路之王】【价格之王】【配置之王】
【免费之王】【香港首推】【梯子之王】【独服之王】
mysql update 使用 select 的结果的方法

在 sql server 中,我们可以使用下面的 update 语句对表进行更新操作。

update t1 set t1.xx= (select yy from t2) ;

但是 mysql 不支持 update set 直接使用 select 的结果,那么怎么在 mysql update 中使用 select 的结果呢?

我们可以换一种解决方法,也就是使用 inner join,上面的 sql 语句在 mysql 中可以改写成下面方式:

update t1 inner join (select yy from t2) t3 set t1.xx = t2.yy

示例:

student 表:

id  name  class_id

1  张三  班级 1

2  李四  班级 2

class 表:

id  name

1  班级 1

2  班级 2

3  班级 3

通过 update inner join 更新 student 表中的 class_id 字段,由班级名称更新为班级 id,具体 mysql 语句为:

update student inner join (select id,name from class) c set student.class_id = c.id where student.class_id = c.name

猜你可能想看的VPS


转载请注明原文地址:http://140.238.13.167:12355/read-217310.html

下一篇       上一篇