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