在我的表中,我有日期列,其上有默认值TIMESTAMP。我想每小时计算一次我的记录。我像这样使用了sproc
SELECT username,
COUNT(IF(HOUR(date)=8,1,NULL) and (IF(docs != null))) AS '8:30 - 9:00',
COUNT(IF(HOUR(date)=9,1,NULL) and (IF(docs != null))) AS '9:00 - 10:00',
COUNT(IF(HOUR(date)=10,1,NULL) and (IF(docs != null))) AS '10:00 - 11:00',
COUNT(IF(HOUR(date)=11,1,NULL) and (IF(docs != null))) AS '11:00 - 12:00',
COUNT(IF(HOUR(date)=12,1,NULL) and (IF(docs != null))) AS '12:00 - 1:00',
COUNT(IF(HOUR(date)=13,1,NULL) and (IF(docs != null))) AS '1:00 - 2:00',
COUNT(IF(HOUR(date)=14,1,NULL) and (IF(docs != null))) AS '2:00 - 3:00',
COUNT(IF(HOUR(date)=15,1,NULL) and (IF(docs != null))) AS '3:00 - 4:00',
COUNT(IF(HOUR(date)=16,1,NULL) and (IF(docs != null))) AS '4:00 - 5:00',
COUNT(disblid) 'Total'
FROM claimloans
group by username;
没有
and (IF(docs != null))
部分我的sproc工作完美。如果行doc值为null,我不想计算行。我怎么能做到这一点?
这是我的工作程序
SELECT username,
COUNT(IF(HOUR(date)=8,1,NULL)) AS '8:30 - 9:00',
COUNT(IF(HOUR(date)=9,1,NULL)) AS '9:00 - 10:00',
COUNT(IF(HOUR(date)=10,1,NULL)) AS '10:00 - 11:00',
COUNT(IF(HOUR(date)=11,1,NULL)) AS '11:00 - 12:00',
COUNT(IF(HOUR(date)=12,1,NULL)) AS '12:00 - 1:00',
COUNT(IF(HOUR(date)=13,1,NULL)) AS '1:00 - 2:00',
COUNT(IF(HOUR(date)=14,1,NULL)) AS '2:00 - 3:00',
COUNT(IF(HOUR(date)=15,1,NULL)) AS '3:00 - 4:00',
COUNT(IF(HOUR(date)=16,1,NULL)) AS '4:00 - 5:00',
COUNT(disblid) 'Total'
FROM claimloans
group by username;
答案 0 :(得分:0)
Nothing is ever equal to null(x = null
和x != null
都是假的),因此您需要将docs != NULL
更改为docs IS NOT NULL
。
答案 1 :(得分:0)
所以,这个表达式:
COUNT(IF(HOUR(date)=8,1,NULL))
当1
的小时数为8时,计算date
这是一个硬编码的非空表达式。如果将1
替换为doc
:
COUNT(IF(HOUR(date)=8,doc,NULL))
该函数还将根据doc
的内容进行计数。也就是说,它不仅在小时为8时,而且在doc
同时不为空时计算行数。
答案 2 :(得分:0)
感谢所有帮助过我的人。有了你的帮助,我制作了追随我的东西。
DELIMITER $$
CREATE DEFINER=`root`@`localhost` PROCEDURE `hourcounter`(IN datestamp DATE)
BEGIN
SELECT username,
COUNT(IF(HOUR(modifytime)=8,1,NULL)) AS '8:30 - 9:00',
COUNT(IF(HOUR(modifytime)=9,1,NULL)) AS '9:00 - 10:00',
COUNT(IF(HOUR(modifytime)=10,1,NULL)) AS '10:00 - 11:00',
COUNT(IF(HOUR(modifytime)=11,1,NULL)) AS '11:00 - 12:00',
COUNT(IF(HOUR(modifytime)=12,1,NULL)) AS '12:00 - 1:00',
COUNT(IF(HOUR(modifytime)=13,1,NULL)) AS '1:00 - 2:00',
COUNT(IF(HOUR(modifytime)=14,1,NULL)) AS '2:00 - 3:00',
COUNT(IF(HOUR(modifytime)=15,1,NULL)) AS '3:00 - 4:00',
COUNT(IF(HOUR(modifytime)=16,1,NULL)) AS '4:00 - 5:00',
COUNT(docs) 'Total'
FROM claimloans
WHERE docs != '' and DATE(date) = datestamp
group by username;
END
感谢所有帮助过我的人。我从你的答案中学到很多东西!!