SQL26 计算25岁以上和以下的用户数量 if函数 或者 case条件 或者 联合表
2021/10/22 19:09:50
本文主要是介绍SQL26 计算25岁以上和以下的用户数量 if函数 或者 case条件 或者 联合表,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
牛客网打卡 : SQL26 计算25岁以上和以下的用户数量
select '25岁以下',count(device_id) number from user_profile where age<25 or age is null union all select '25岁及以上',count(device_id) number from user_profile where age>=25
使用联合表固然可以,但是还是属于麻烦
使用if函数,或者是使用case when end进行组合
select if(age>=25,'25岁及以上','25岁以下') as age_cut, count(device_id) number from user_profile group by age_cut
使用case when end
select (case when age>=25 then '25岁及以上' else '25岁以下' end) as age_cut, count(device_id) from user_profile group by age_cut
if函数学习
if(条件,结果,反面结果) (可选,as 列名)
case when end 学习
使用case关键字进行声明:
case when 条件 then 结果 else 反面结果 end 可选,as 新列名
这篇关于SQL26 计算25岁以上和以下的用户数量 if函数 或者 case条件 或者 联合表的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-09-28pyqt 怎么打包整个项目-icode9专业技术文章分享
- 2024-09-28laravel Commands 创建带有参数的 Artisan 命令的步骤和示例-icode9专业技术文章分享
- 2024-09-28antd怎么实现渲染tiff图片-icode9专业技术文章分享
- 2024-09-28英文半角中划线和中文全角的中划线有什么区别-icode9专业技术文章分享
- 2024-09-28nvm npm 和node 他们之间有什么关系-icode9专业技术文章分享
- 2024-09-28Node Version Manager (nvm)使用教程-icode9专业技术文章分享
- 2024-09-28nvm命令太慢,是什么原因-icode9专业技术文章分享
- 2024-09-28Kotlin 如何增加、删除和修改 MutableStateFlow 中的值。-icode9专业技术文章分享
- 2024-09-28Kotlin的stateFlow.update 写法介绍-icode9专业技术文章分享
- 2024-09-28kotlin 怎么获取当前时间格式-icode9专业技术文章分享