mysql - 从多个表中获取计数
问题描述
我有 3 个表,它们有一个共同的 'type_id' 列。它是来自 Table_type 的外键
Table_1 id,type_id,date... Table_2 id,type_id,date... Table_3 id,type_id,date...
Table_Type id、type_name、描述
select type_id, count(*) from Table_1 GROUP BY type_id
给出单个表的计数.
我想知道特定类型使用了多少次。
type_id,计数(来自 3 个表) 1 , 10 2 , 5 3. , 3
解决方案
使用union all
和group by
:
select type_id, count(*)
from ((select type_id from table_1) union all
(select type_id from table_2) union all
(select type_id from table_3)
) t
group by type_id
推荐阅读
- javascript - 如何从表格中使用 Google 脚本获取值(),以便日期值匹配
- r - H2O h2o.stackedEnsemble 中的错误:“不知道如何确定多项分类器的分布”
- oauth-2.0 - 如何使用身份验证服务器颁发的 JWT 令牌来允许访问另一个应用程序?
- makefile - 允许用户覆盖 CFLAGS、CXXFLAGS 和好友
- swift - Travis swift库编译错误
- swift - 将 3D 模型及其节点导入 Xcode
- java - 通过 maven 将环境变量传递给 config.properties 文件
- c# - C# REST API,模型的子节点应该是同一类型,并且路由必须递归定义
- java - 如何比较两个哈希图
- wordpress - 在wordpress中提交表单后如何显示成功消息?