mySQL查询抓取多个时间帧

时间:2016-02-23 17:23:30

标签: mysql sql

我目前正在使用4个查询来获取4个不同时间范围内的出现次数。

jquery.ui

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

2 个答案:

答案 0 :(得分:3)

组合查询的更好方法

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 

答案 1 :(得分:0)

您可以使用union

SELECT  'AB',  count(*) WHERE keyword='keyword' AND time>A AND time<B;
UNION
SELECT  'BC', count(*) WHERE keyword='keyword' AND type>B AND time<C;
UNION
SELECT  'CD', count(*) WHERE keyword='keyword' AND type>C AND time<D;
UNION
SELECT  'DE', count(*) WHERE keyword='keyword' AND type>D AND time<E;