在转换日期格式时的php mysql案例

时间:2014-07-25 16:44:16

标签: php mysql

这是正常的mysql查询

SELECT SUM( Minutes ) as minsDecPrac, COUNT( DISTINCT (CAST(dtDateTime as DATE))) as playDecPrac FROM `tbl_atschool_timelog` WHERE `intuid` = 48876 AND `intGametypeId` = 2 AND `intGame` = 1 

这是输出

minsDecPrac     playDecPrac   
17                 1

以下是使用CASE WHEN

的查询
SELECT SUM(CASE WHEN intGametypeId = 2 AND intGame =1 THEN Minutes ELSE 0 END) AS minsDecPrac COUNT(DISTINCT CASE WHEN intGametypeId = 2 AND intGame =1 THEN  (CAST(dtDateTime as DATE)) ELSE 0 END) AS playDecPrac FROM `tbl_atschool_timelog` WHERE `intuid` = 48876

这是输出

minsDecPrac     playDecPrac   
17                 2

问题是如果我使用CASE WHEN,那么" playDecPrac"(dtDateTime)的输出是错误的。 我不知道自己错了什么。如果有人知道,请帮帮我

1 个答案:

答案 0 :(得分:1)

此条款在此处:

COUNT(DISTINCT CASE WHEN intGametypeId = 2 AND intGame =1 THEN  (CAST(dtDateTime as DATE)) ELSE 0 END)
除了日期之外,

ELSE 0计算0作为不同的值。所以COUNT(DISTINCT )比它应该高一个。

您可以尝试ELSE NULL