dplht39359 2014-07-25 16:44
浏览 23
已采纳

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

Here is the normal mysql query

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 

Here is the Output

minsDecPrac     playDecPrac   
17                 1

Here is the query using 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

Here is the Output

minsDecPrac     playDecPrac   
17                 2

The problem is that if i use CASE WHEN, the output of "playDecPrac"(dtDateTime ) is wrong. I don't know what i am wrong. Please help me if anybody knows

  • 写回答

1条回答 默认 最新

  • douxiezha9319 2014-07-25 16:59
    关注

    This clause here:

    COUNT(DISTINCT CASE WHEN intGametypeId = 2 AND intGame =1 THEN  (CAST(dtDateTime as DATE)) ELSE 0 END)
    

    counts the 0 from ELSE 0 as a distinct value, in addition to the dates. So the COUNT(DISTINCT ) is one higher than it should be.

    You might try ELSE NULL.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度