2013-06-01 49 views
0

我想用一个公用密钥计算每个表格的总列数。我可以用工会声明做。我怎么可以使用加入加入4个表格并从每个表格收集计数

查询:

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' 

但它显示错误计数

回答

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 
+0

其获取foll欠缺错误#1064 - 您的SQL语法错误;检查对应于您的MySQL服务器版本的手册,以在'*)AS WOWcount,COUNT(b。*)AS后记计数,COUNT(c。*)AS CommentCount,COUNT(d。*'at line 1 – SSK

+0

@ user2345691我已更新我的回答,请检查它 – Fabio