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

时间:2022-03-11 11:59:54

I am trying count total column from each table with one common key. I can do with union statement. how i can use join

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

Query :

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'

I have used following Query :

我使用了以下查询:

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' 

But it displaying wrong count

但它显示错误的计数

1 个解决方案

#1


0  

You can use an INNER JOIN query

您可以使用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

#1


0  

You can use an INNER JOIN query

您可以使用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