首页 > 解决方案 > 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 的单元。
任何人都知道这样做吗?

标签: sql

解决方案


尝试这个:

SELECT status_id, COUNT(*) AS cnt
FROM yourTable
WHERE status_id IN (1, 2, 3)
GROUP BY status_id;

推荐阅读