首页 > 解决方案 > case when...then 1 else 0

问题描述

I just learn SQL for one month and have a question that what does it mean about 1 and 0 in the subquery 'case when...then 1 else 0'? I have google for similar questions, and it says 1 and 0 just means plus 1 and plus 0. But I can't understand what it means. Does it mean that the result that code runs will be plus one or plus zero? Thank you very much!

标签: sql

解决方案


并非如此,这意味着查询的结果将为 1 或 0。

不是加一或加零的某种表达,而只是一或零。作为一个简单的数字。

例子:

我们要指出,年龄大于 18 岁的人属于 1 号人的范畴,而年龄小于 18 岁的人属于 0 号人的范畴。

询问:

SELECT [Name],
    CASE
    WHEN [Age] >= 18 THEN 1
    ELSE 0
    END AS [AgeType];

对于数据:

Name|Age
Nick|20
Mark|17
Bob |18

结果将是:

Name|AgeType
Nick|1
Mark|0
Bob |1

推荐阅读