我正在尝试使用以下格式执行SUM()
具有三个类似条件的操作:if()
第一:
if((TIME_TO_SEC(TIMEDIFF(IF(TIME = '0000-00-00 00:00:00',sysdate() , TIME ), TIME2)) > high OR TIME IS NULL) AND TIME3 != '0000-00-00 00:00:00' AND DATE_FORMAT(TIME2, '%Y-%m-%d %H:%i') < DATE_FORMAT(now(), '%Y-%m-%d %H:%i')
第二个:
if(TIME_TO_SEC(TIMEDIFF(IF(TIME = '0000-00-00 00:00:00',sysdate() , TIME ), TIME2)) > value AND TIME3 != '0000-00-00 00:00:00' AND DATE_FORMAT(TIME2, '%Y-%m-%d %H:%i') < DATE_FORMAT(now(), '%Y-%m-%d %H:%i')
第三个:
if(TIME_TO_SEC(TIMEDIFF(IF(TIME = '0000-00-00 00:00:00',sysdate() , TIME ), TIME2)) >0 AND TIME3 != '0000-00-00 00:00:00' AND DATE_FORMAT(TIME2, '%Y-%m-%d %H:%i') < DATE_FORMAT(now(), '%Y-%m-%d %H:%i')
存在high
和value
来自几个表的两个不同字段列。
不幸的是,我似乎无法完成这项工作,很可能是因为这句话不能SUM()
放在一起。
如何才能做到这一点?
编辑:添加了整个查询:
SELECT VALUE1 ,
COUNT( * ) TOTAL
from (
SELECT (
if((TIME_TO_SEC(TIMEDIFF(IF(TIME = '0000-00-00 00:00:00',sysdate() , TIME ), TIME2)) > high OR TIME IS NULL) AND TIME3 != '0000-00-00 00:00:00' AND DATE_FORMAT(TIME2, '%Y-%m-%d %H:%i') < DATE_FORMAT(now(), '%Y-%m-%d %H:%i')) +
if(TIME_TO_SEC(TIMEDIFF(IF(TIME = '0000-00-00 00:00:00',sysdate() , TIME ), TIME2)) > value AND TIME3 != '0000-00-00 00:00:00' AND DATE_FORMAT(TIME2, '%Y-%m-%d %H:%i') < DATE_FORMAT(now(), '%Y-%m-%d %H:%i')) +
if(TIME_TO_SEC(TIMEDIFF(IF(TIME = '0000-00-00 00:00:00',sysdate() , TIME ), TIME2)) >0 AND TIME3 != '0000-00-00 00:00:00' AND DATE_FORMAT(TIME2, '%Y-%m-%d %H:%i') < DATE_FORMAT(now(), '%Y-%m-%d %H:%i'))) as 'ok',
if((TIME_TO_SEC(TIMEDIFF(IF(TIME = '0000-00-00 00:00:00',sysdate() , TIME ), TIME2)) > high OR TIME IS NULL) AND DATE_FORMAT(TIME2, '%Y-%m-%d %H:%i') < DATE_FORMAT(now(), '%Y-%m-%d %H:%i'),4,
if(TIME_TO_SEC(TIMEDIFF(IF(TIME = '0000-00-00 00:00:00',sysdate() , TIME ), TIME2)) > value AND DATE_FORMAT(TIME2, '%Y-%m-%d %H:%i') < DATE_FORMAT(now(), '%Y-%m-%d %H:%i'),3,
if(TIME_TO_SEC(TIMEDIFF(IF(TIME = '0000-00-00 00:00:00',sysdate() , TIME ), TIME2)) >0 AND DATE_FORMAT(TIME2, '%Y-%m-%d %H:%i') < DATE_FORMAT(now(), '%Y-%m-%d %H:%i'),2,
if(TIME >'0000-00-00 00:00:00', 1,0) ))) VALUE1
FROM table1 , table2 left outer join table3 on( value2 = value3 ) where id_param='col1' and col2 = DATE_FORMAT(if(CURTIME()>='00:00:00' and CURTIME() < '08:00:00',sysdate() - INTERVAL 1 DAY, sysdate()), '%Y-%m-%d') ) as datos GROUP BY VALUE1
这应该返回一个 2columnx3rows 表,其值为“ok”、“4”、“3”和“2”。