为了完成综合多个表统计用户的信息,我想用多个表联合起来,但是网上没有太满意的答案,采取了以下方法,可以成功获取。特此记录并分享,但是感觉自己的方法效率可能比较低。如有更好方法,如不吝赐教将不胜感激!
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
本文分享了一种通过多个表联合查询来统计特定用户信息的方法,包括基本信息、信息数量、建筑数量、评论信息数量和评论建筑数量。使用左连接(left join)确保所有信息被包含在最终结果中。
2万+

被折叠的 条评论
为什么被折叠?



