表
需求:
一条sql查出time在2022-01-02 到2022-01-05 的数据数量 以及time在2022-01-05 到 2022-01-10 的数据数量
sql
SELECT
count(case when time BETWEEN '2022-01-05' and '2022-01-10' then time end) as "05-10",
count(case when time BETWEEN '2022-01-01' and '2022-01-05' then time end) as "01-05"
FROM
`teacher`
结果:
case 是用于过滤需要计数的数据的条件。