数据库的数据处理

场景一: 单表中新增一个字段,将该表中的字段updatetime(datetime类型)的值赋到update_at(bigint类型)上。

通过等值链接(inner join)

update `city` c inner join `city` c1 on c.`city_id` = c1.`city_id` set c.`update_at` = UNIX_TIMESTAMP(c1.`updatetime`) * 1000

场景二: 给表cs添加新字段grade(年级),将grade赋为另一个表s中的grade,两表关联关系为student_id

通过left join 链接

update t_course_score cs left join t_student s on cs.student_id = s.id set cs.grade = s.grade

场景三:通过用户id集合,给用户账号表设置账号过期时间(涉及account表和user表),不能直接select一个表后update同一个表,会报错,需将select后的结果作为中间表然后取select

失效时间:2022.6.1
update uranus_base.auth_account set expire_at = UNIX_TIMESTAMP('2022-06-01') * 1000 where id in (select id from (select acc.id from auth_account acc left join org_member mem on acc.id = mem.account_id where classrade_id = '352cb9da5d744d7883c4548632a5f128'
) as account);
 
 
 
失效时间:2022.5.22
update uranus_base.auth_account set expire_at = UNIX_TIMESTAMP('2022-05-22') * 1000 where id in (select id from (select acc.id from auth_account acc left join org_member mem on acc.id = mem.account_id where classrade_id = 'd623e954139042f0ac175f96740325c3'
) as account);

你可能感兴趣的:(SQL,sql)