【问题标题】:mysql count and joinmysql 计数和加入
【发布时间】:2014-12-20 18:29:13
【问题描述】:

我想从多个表和连接中检索具有总计数的表

我有 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

期待最好的方法......

【问题讨论】:

  • 你试过什么?您是否设法将这些表连接在一起?你知道group by 子句是什么吗?
  • 我想我正在尝试将表 wp_tutorials 和 wp_tut_users 加入总用户数。

标签: mysql sql wordpress


【解决方案1】:

使用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

【讨论】:

  • 是的。代码已经过测试并且可以工作。这是我所期望的。谢谢
猜你喜欢
  • 2012-06-07
  • 2012-06-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-19
  • 1970-01-01
  • 2012-11-03
  • 1970-01-01
相关资源
最近更新 更多