修改前数据:

需求:将小盒子学院中拥有两个学号以上学生的学号前面加上前缀“hr”

今天我像以前操作mysql写了一个update sql:

update edu_user_copy set stuNum=concat('hr',stuNum) where stuNum in (select stuNum from edu_user_copy 
where company like '小盒子学院%' group by stuNum having count(stuNum)>1);

根据子查询的结果,更新表中的一个字段。

在mysql数据库中执行后报错:

一直没弄明白这个错误,然后经过多次度娘后,发现mysql update时,更新的表不能在set和where中用于子查询。

修改上面的sql为mysql数据库支持的方式:

update edu_user_copy du,(select stuNum from edu_user_copy 
where company like '小盒子学院%' group by stuNum having count(stuNum)>1) b
set du.stuNum=concat('hr',b.stuNum)
where du.stuNum=b.stuNum;

修改后数据:

ok,大功告成!

 

Logo

更多推荐