MySQL TIME函数意外结果

时间:2017-10-05 04:47:12

标签: mysql

无论日期范围有多大,我都需要能够在指定时间之间提取数据。例如,仅在日期范围内的每一天的10:00到11:00之间检索记录。

我以为我已将其固定,直到用户指定的时间跨越午夜并且不返回任何数据。

这是我的查询,不返回任何数据(使用DATE和TIME函数):

SELECT
 an.descr_agente as Agent,
 COUNT(if(ql.queue IN (9100, 9102, 9104),1, null)) as UK,
 COUNT(if(ql.queue IN (9200, 9202, 9204),1, null)) as AU,
 COUNT(if(ql.queue IN (9400, 9402, 9404),1, null)) as CA
FROM queue_log ql
LEFT JOIN agenti_noti an
 ON ql.agent = an.nome_agente
WHERE ql.partition = 'P001'
AND DATE(from_unixtime(ql.time_id)) BETWEEN '2017-10-03' AND '2017-10-04'
AND TIME(from_unixtime(ql.time_id)) BETWEEN '16:00' AND '01:01'
AND ql.verb IN ('COMPLETECALLER','COMPLETEAGENT','TRANSFER')
AND an.descr_agente IS NOT NULL
GROUP BY an.descr_agente

如果我将其更改为下面,它可以正常工作,但当用户将日期范围增加超过1天时,它当然会有不正确的数据。

SELECT
 an.descr_agente as Agent,
 COUNT(if(ql.queue IN (9100, 9102, 9104),1, null)) as UK,
 COUNT(if(ql.queue IN (9200, 9202, 9204),1, null)) as AU,
 COUNT(if(ql.queue IN (9400, 9402, 9404),1, null)) as CA
FROM queue_log ql
LEFT JOIN agenti_noti an
 ON ql.agent = an.nome_agente
WHERE ql.partition = 'P001'
AND from_unixtime(ql.time_id) BETWEEN '2017-10-03 16:00' AND '2017-10-04 01:01'
AND ql.verb IN ('COMPLETECALLER','COMPLETEAGENT','TRANSFER')
AND an.descr_agente IS NOT NULL
GROUP BY an.descr_agente

我假设TIME功能不会自动重叠到第二天。我该怎么做才能正常工作?

2 个答案:

答案 0 :(得分:1)

//for each item in array v run process 
//  but only run the next item in v when the first item is resolved
var processValuesSerial = (v,process) => {
  //checking valid imput
  if(v === undefined){return Promise.resolve("nothing to do"); }
  if(v.length === undefined){return Promise.resolve("nothing to do"); }
  if(v.length === 0){return Promise.resolve("nothing to do"); }
  //only one item, no need to reduce
  if(v.length === 1){return process(Promise.resolve(v[0]),v[0]); }
  //at least 2 items in v, process them
  return v
    .map(x => Promise.resolve(x))
    .reduce(process)
    //last item will not be passed to process function in reduce
    //  manually execute the last process function on this item
    .then(
      x=>
        process(
          Promise.resolve(x)
          ,Promise.resolve(v.slice(-1))
        )
    )
}
//functions that do something
var logValue =
  (value,message) =>
    //log value and return it
    console.log(message,value) || value
//asynchronous function
var waitFor = 
  (howLong,returnValue) =>
    //returning a promise that resolves after waiting for "howLong"
    new Promise(
      (resolve,reject)=>
        setTimeout(x => resolve(returnValue),howLong)
    )
;


//for each value I would like to do something asynchronous.
//  for example
var values = [1,2,3,4,5];

/**
 * Problem with promises is that they start immediately so the 
 * following example will start 1,2,3,4,5 imediately and then
 * what we want is do(1).then(do(2)).then(do(3))
 */
Promise.all(
  values.map(
    x =>
      Promise.resolve(x)
      .then(
        x => logValue(x,"+++++At the same time Starting:")
      )
      .then(
        x => waitFor(x*500,x)
      )
      .then(
        x => logValue(x,"+++++At the same time Finished:")
      )
  )
)
.then(x => console.log("finished promises at the same time"))

//the following will not start everything at the same time
//   it will do(1).then(do(2)).then(do(3))
processValuesSerial(
  values
  //reducer function, acc is current value as a promise
  //  item is the next value as a promise
  ,(acc,item)=>
    //acc is a promise that resolves to "item" (=1 then 2 then 3 then 4 then 5)
    //  the first time it is 1, the second time it is whatever we return here
    //  we return a promise
    acc.then(
      //acc is resolved so we should have a value of 1, then 2 ...
      //  if we return the right value
      x=>logValue(x,"XXXXX In serie Starting:")
    )
    .then(
      //return a promise that waits
      x=>waitFor(x*500,x)
    )
    .then(
      x=>logValue(x,"XXXXX In serie Finished:")
    )
    //return next item
    .then(x=>item)
)
"OOOOOO Finished running synchronous code"

这可能需要一些UI更改或其他功能来检测交叉并创建拆分

答案 1 :(得分:0)

我不是专家,但你不能使用date_format吗?类似于:DATE_FORMAT(FROM_UNIXTIME(ql.time_id),'%Y-%m-%d%H:%i')BETWEEN'2017-10-03 16:00'和'2017-10-04 01:01'