日期:2014-05-19  浏览次数:20406 次

case when then的问题?
sum(case   parentId   when   0   then   1   else   0   end)
sum(case   when   parentId=0   then   0   else   1   end)
这两句是啥意思?有什么不同?

------解决方案--------------------
sum(case parentId when 0 then 1 else 0 end)
统计 parentId=0 的记录条数

sum(case when parentId=0 then 0 else 1 end)
统计 parentId <> 0 的记录条数

------解决方案--------------------
lixk2000() ( ) 信誉:100 Blog 加为好友 2007-06-07 08:43:43 得分: 0


每一个是:
计算parentId=0的记录条数
每二个是:
计算parentId <> 0的记录条数

为什么是这个结果?


-----------
上面不是有解釋嗎?


sum(case parentId when 0 then 1 else 0 end)

逐條對比表中每條紀錄,如果parentId=0的话,就加上1 如果parentId <> 0 ,就加上0,最後的到的統計結果就是“计算parentId=0的记录条数”。