数据库进阶·如何针对所有用户数据中没有的数据去加入随机的数据-蜻蜓Q系统用户没有头像如何加入头像数据-优雅草科技kir

数据库进阶·如何针对所有用户数据中没有的数据去加入随机的数据-蜻蜓Q系统用户没有头像如何加入头像数据-优雅草科技kir

本内容主要用于学习,头像只是举例的一个字段,可以应用在其他方面,举一反三

问题背景:

蜻蜓q旗舰版是根据手机设备信息自动分配用户账号并登陆,跳过了用户注册上传头像步骤,因此大量用户没有头像,

此时需要给这些用户添加上随机的头像整理以下运行语句:

update users
set avatar='http://qingtingcun.youyacao.com/qiniu_202110270837150713870048.png'
where id IN(
select A.* from(select id from users where avatar='' order by rand() limit 10) AS A
);

这段语句是每次随机找出10条没有头像的进行更新为指定头像地址:

http://qingtingcun.youyacao.com/qiniu_202110270837150713870048.png

那么如果我们要随机更新不同的几个头像那么代码则为

update users
set avatar='图片1'
where id IN(
select A.* from(select id from users where avatar='' order by rand() limit 10) AS A
);
update users
set avatar='图片2'
where id IN(
    select A.* from(select id from users where avatar='' order by rand() limit 10) AS A
);
update users
set avatar='图片3'
where id IN(
    select A.* from(select id from users where avatar='' order by rand() limit 10) AS A
);

诸如此就是随机更新为这三个头像,以此类推,如果多放几个头像多执行几次很快就ok了。

效果

多执行几次,直到提示有0行被影响说明所有用户都已经加上头像了。

猜你喜欢

转载自blog.csdn.net/dujiangdu123/article/details/125909889
今日推荐