gpt4 book ai didi

mySQL 查询以获取多个时间范围

转载 作者:行者123 更新时间:2023-11-29 01:53:19 27 4
gpt4 key购买 nike

我目前使用 4 个查询来获取 4 个不同时间范围内的事件计数。

SELECT count(*) WHERE keyword='keyword' AND time>A AND time<B;
SELECT count(*) WHERE keyword='keyword' AND type>B AND time<C;
SELECT count(*) WHERE keyword='keyword' AND type>C AND time<D;
SELECT count(*) WHERE keyword='keyword' AND type>D AND time<E;

我认为必须有一种方法可以将它们合并到一个查询中。有什么想法吗?

最佳答案

更好的组合查询的方法

SELECT CASE
WHEN time > A AND time < B THEN 'A-B'
WHEN time > B AND time < C THEN 'B-C'
WHEN time > C AND time < D THEN 'C-D'
WHEN time > D AND time < E THEN 'D-E'
END,
Count(*)
FROM yourtable
WHERE keyword = 'keyword'
AND time > A
AND time < E
GROUP BY CASE
WHEN time > A AND time < B THEN 'A-B'
WHEN time > B AND time < C THEN 'B-C'
WHEN time > C AND time < D THEN 'C-D'
WHEN time > D AND time < E THEN 'D-E'
END

关于mySQL 查询以获取多个时间范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35584127/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com