MySQL按年龄段查询_MySQL
Jun 01, 2016 pm 01:17 PM下面是项目中按照男女年龄段统计的核心sql代码:
1 |
|
我拿其中一句解释一下
SUM(CASE WHEN s.sex=1 AND tr.age >=18 AND tr.age
先看sum的括号里面的部分
case when sex = 1 and age>=18 and age
它表示的含义是:如果性别为1(也就是男),并且年龄在18-29岁之间成立为1,不成立为0.
case和end 是一个关键字你可以理解为语句的开始和结束。
when相当于if做判断,then就是判断之后显示的结果。如果成立显示为1,不成立显示为0
sum就是将各个值相加。形如:1+1+0+1+0+1+1+0+1+1
项目最后统计的结果截图形如:

Hot Article

Hot tools Tags

Hot Article

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Can AI conquer Fermat's last theorem? Mathematician gave up 5 years of his career to turn 100 pages of proof into code

A closer look at PyCharm: a quick way to delete projects

Share an easy way to package PyCharm projects

PyCharm Practical Tips: Convert Project to Executable EXE File

How to implement data statistics and analysis in uniapp

How to Make a Shopping List in the iOS 17 Reminders App on iPhone

How to use SQL statements for data aggregation and statistics in MySQL?

How to quickly build a statistical chart system under the Vue framework
