Boolean Logic in Select Statement

This question already has an answer here:

  • How do I perform an IF…THEN in an SQL SELECT? 22 answers
  • How do I return a boolean value from a query during condition evaluation? 4 answers

  • SELECT t.[Key]
          ,t.[Parent_Key]
          ,t.[Parent_Code]
          ,t.[Code]
          ,t.[Desc] 
          ,t.[Point]
          ,CASE WHEN t.[Point] > 2 THEN 1 ELSE  
                CASE WHEN t.[Point] = 1 THEN 0 ELSE NULL END 
           END AS [isChild]
          ,t.[By] 
          ,t.[On]
    FROM [db].[stats] t WHERE t.[Parent_Key]= @tmpParameter
    

    请注意,当t [Point] <1时,[isChild]将为空


    凯斯是你的朋友...

     SELECT Key, Parent_Key, Parent_Code, Code, Desc, point, 
         case when point > 2 then 1 
              when point = 1 then 0 end isChild, 
         [By], [On]
     FROM db.stats  
     WHERE Parent_Key= @tmpParameter
    

    使用case语句:

    SELECT t.[Key]
          ,t.[Parent_Key]
          ,t.[Parent_Code]
          ,t.[Code]
          ,t.[Desc] 
          ,t.[Point]
          ,CASE t.[Point] WHEN 1 THEN FALSE WHEN 2 THEN TRUE END as [isChild]
          ,t.[By] 
          ,t.[On]
    FROM [db].[stats] t WHERE t.[Parent_Key]= @tmpParameter
    
    链接地址: http://www.djcxy.com/p/94342.html

    上一篇: 基于第三列动态选择两列中的一列

    下一篇: 选择语句中的布尔逻辑