sql - SQL,如何使用计算相同表结构但不同ID中的单元数
问题描述
unit_table
Unit_id Status_id
1 1
2 2
3 2
4 3
5 3
上面显示了一个具有 2 个不同表结构的示例表,即 unit_id 和 status_id,现在我想编写一个查询来找出 status_id 为 1,2 或 3 的单元有多少。我编写了如下所示的 SQL:-
select count(unit_id)
where status_id =1 ..
但是我被困在这里..因为我不知道如何使用 where 子句来计算具有不同状态 ID 的单元。
任何人都知道这样做吗?
解决方案
尝试这个:
SELECT status_id, COUNT(*) AS cnt
FROM yourTable
WHERE status_id IN (1, 2, 3)
GROUP BY status_id;
推荐阅读
- arrays - 在写入 .json 文件之前,使用 toJSON() 从对象数组中过滤出循环引用
- java - 选择/取消选择表格中的多行
- php - Laravel eloquent 合并两个结果并排序
- python - 按 DataFrame 的另一列移动列分组的值
- ruby-on-rails - 运行“捆绑安装”,但 Rails 一直告诉我找不到 gem
- javascript - 可以从另一个站点调用源映射吗
- php - PHP:当我在 xml 中转换时出现问题
- r - 根据标准创建子集矩阵/根据标准提取关键行
- python - 张量流中的保真度自定义损失函数 - 输入形状问题
- date - SSAS 日期格式