首页 > 数据库技术 > 详细

MySQL:inner join的使用方法

时间:2021-05-31 16:02:09      阅读:10      评论:0      收藏:0      [点我收藏+]

场景

????????用户信息表ri_building_unit_user中字段key_person_type代表用户是红码还是黄码,fk_building_unit_id字段代表是房屋的编码,统计每一个房屋中红码人员总数,写入房屋表ri_building_unit字段key_person_red_num。用户信息表ri_building_unit_user字段fk_building_unit_id对应房屋表ri_building_unit字段id

解决方案

update ri_building_unit as a inner join (select fk_building_unit_id, count(*) as countnum from ri_building_unit_user where key_person_type = 2 group by fk_building_unit_id) as b on a.id = b.fk_building_unit_id?

set a.key_person_yellow_num= b.countnum


update ri_building_unit as a inner join (select fk_building_unit_id, count(*) as countnum from ri_building_unit_user where key_person_type = 1 group by fk_building_unit_id) as b on a.id = b.fk_building_unit_id?

set a.key_person_red_num = b.countnum

注意:

提供下耗时最大的一个测试例子:

update ri_building_unit as a set key_person_yellow_num=(select count(*) from ri_building_unit_user as b? where a.id = b.fk_building_unit_id and b.key_person_type = 2)?


MySQL:inner join的使用方法

原文:https://blog.51cto.com/fengyuzaitu/2835306

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!