sql - 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!
解决方案
并非如此,这意味着查询的结果将为 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
推荐阅读
- arrays - 将数组拆分为所有可能的大小相等的数组
- python - 根据图像调整边界框大小
- node.js - 尝试在 react-native 中初始化新项目时,我收到意外的标识符作为语法错误
- python - 应用词袋
- php - 如何在 Laravel 关系中进行 INNER JOIN?
- javascript - Angular 代码未在浏览器中显示结果
- wordpress - 如何使用 Duplicator 插件小心地将旧站点 URL 迁移到 WordPress 中的新 URL?
- gitlab - Gitlab-当我使用 API 调用设置估计时间时出现 404 未找到错误
- java - 使用 jQuery/Ajax 将多个选中的复选框发送到 Java
- python - 使用天空场找到行星/卫星的平均节点