mysql 多表联合查询,两个、三个、四个表join

为了完成综合多个表统计用户的信息,我想用多个表联合起来,但是网上没有太满意的答案,采取了以下方法,可以成功获取。特此记录并分享,但是感觉自己的方法效率可能比较低。如有更好方法,如不吝赐教将不胜感激!

select a.*,b.information_count, c.building_count, d.comment_information_count, e.comment_building_count from 
(select id,points from user where id = 123) as a 
LEFT JOIN 
(select user_id,count(*) as information_count from information where user_id = 123 ) as b 
on a.id = b.user_id
left join 
(select user_id, count(*) as building_count from building where user_id = 123) as c
on a.id = c.user_id
left join 
(select user_id, count(*) as comment_information_count from comment1 where user_id = 123) as d
on a.id = d.user_id
left join 
(select user_id, count(*) as comment_building_count from comment2 where user_id = 123) as e
on a.id = e.user_id

 

  • 0
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值