爲了完成綜合多個表統計用戶的信息,我想用多個表聯合起來,但是網上沒有太滿意的答案,採取了以下方法,可以成功獲取。特此記錄並分享,但是感覺自己的方法效率可能比較低。如有更好方法,如不吝賜教將不勝感激!
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