加入4个表并从每个表中收集计数

时间:2013-06-01 11:46:39

标签: mysql

我正在尝试使用一个公共密钥计算每个表中的总列数。我可以用工会声明。我如何使用加入

查询:

SELECT count(id) As WOWcount FROM `wow_track` where author_post_id='882' union SELECT count(*) As Followcount FROM `FolllowUserPost` where postID='882' union SELECT count(*) As CommentCount FROM `f9pix_comments` where post_id_fk ='882' union SELECT count(*)  As ViewCount FROM `viewPhotosTrack` where postID='882'

我使用了以下查询:

SELECT COUNT(a.author_post_id) AS WOWcount, COUNT(b.postID) AS Followcount, COUNT(c.post_id_fk) AS CommentCount, COUNT(d.postID) As ViewCount
FROM wow_track a
LEFT JOIN FolllowUserPost b ON a.author_post_id = b.postID
LEFT JOIN f9pix_comments c ON b.postID = c.post_id_fk
LEFT JOIN viewPhotosTrack d ON c.post_id_fk = d.postID
WHERE a.author_post_id='882' 

但它显示错误的计数

1 个答案:

答案 0 :(得分:0)

您可以使用INNER JOIN查询

SELECT COUNT(a.author_post_id) AS WOWcount, COUNT(b.postID) AS Followcount, COUNT(c.post_id_fk) AS CommentCount, COUNT(d.postID) As ViewCount
FROM wow_track a
LEFT JOIN FolllowUserPost b
ON a.author_post_id = b.postID
LEFT JOIN f9pix_comments c
ON b.postID = c.post_id_fk
LEFT JOIN viewPhotosTrack d
ON c.post_id_fk = d.postID
GROUP BY a.author_post_id