0

我正在尝试使用一个公用键从每个表中计算总列。我可以使用联合声明。我如何使用加入

询问 :

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' 

但它显示错误的计数

4

1 回答 1

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
于 2013-06-01T12:00:27.093 回答