2014-12-20 83 views
0

我想从多个表和连接总数检索表mysql count and join

我有4个wordpress自定义数据库表。

wp_tutorials

ID   tut_name 
1   php    
2   mysql    
3   wordpress 

wp_chapters

ID  tut_id  chapter_name 
1  1   php1 
2  1   php2 
3  2   mysql1 
4  2   mysql2 

wp_series

ID  chapter_id series_name 
1   1   php1A 
2   1   php1B 
3   2   php2A 
4   2   php2B 
5   3   mysql1A 
6   3   mysql1B 
7   4   mysql2A 
8   4   mysql2B 

wp_tut_users

ID series_id  user_name 
1  2    user1 
2  2    user2 
3  4    user3 
4  6    user4 
5  7    user5 

从这四个表我想通过下表的SQL查询,检索

1.教程

tut_name  total_users 
    php    3 
    mysql    2 
    wordpress   0 

期待最好的办法......

+0

你尝试过什么报复教程?你是否设法一起参加了这些表?你知道'group by'子句是什么吗? – Hobo

+0

我想我试图将用户总数与wp_tutorials和wp_tut_users连接起来。 – Nisu

回答

0

使用left join没有用户

select t.tut_name, count(u.id) as total_users 
from wp_tutorials t 
left join wp_chapters c on c.tut_id = t.id 
left join wp_series s on s.chapter_id = c.id 
left join wp_tut_users u on u.series_id = s.id 
group by t.tut_name 
+0

是的。代码已经过测试和工作。这是我所期待的。谢谢 – Nisu