sql-server-2008 - Select 语句中的 bool 逻辑

标签 sql-server-2008 sql

这个问题在这里已经有了答案:





How do I perform an IF...THEN in an SQL SELECT?

(32 个回答)



How do I return a boolean value from a query during condition evaluation?

(4 个回答)


7年前关闭。




如何在查询运行时在 select 语句中实现 bool 逻辑?

SELECT t.[Key]
      ,t.[Parent_Key]
      ,t.[Parent_Code]
      ,t.[Code]
      ,t.[Desc] 
      ,t.[Point]
      ,[isChild] -- If Point > 2, then true, if Point == 1 Then false   
      ,t.[By] 
      ,t.[On]
FROM [db].[stats] t WHERE t.[Parent_Key]= @tmpParameter

我想根据 t.[Point] 做一些逻辑来确定 [isChild] bool 值

最佳答案

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] 将为空

关于sql-server-2008 - Select 语句中的 bool 逻辑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24700288/

相关文章:

sql - SQL 中的双 IN 语句

sql-server - 在生产服务器 (SQL Server 2008) 中添加新列的步骤?

sql-server - 如何为联合结果添加排序依据?

mysql - 大小写计数未给出正确结果

mysql - 日期之间的查询也返回前几个月的行

sql - 根据今天获取下一个工作日的算法

sql - 如何只检查日期时间字段上的时间而忽略日期?

SQL 查询仅返回每个组 ID 1 条记录

mysql - 错误代码 105,无法修复 MySQL

mysql - DESC 语句可以在 SELECT 语句中使用吗?