虚构(自制)组中的MYSQL OrderBy
想要准备一个SQL查询,该查询具有OrderBy的自定义grop 表雇员虚构(自制)组中的MYSQL OrderBy,mysql,database,Mysql,Database,想要准备一个SQL查询,该查询具有OrderBy的自定义grop 表雇员 id| age | name |salary | _ _ __ ___ 1 | 10 | A | 100$ | _ _ __ ___ 2 | 25 | A | 105$ | _ _ __ ___ 3 | 15 | A | 110$ | _ _ __ ___ 4 | 22 | A | 150$ |
id| age | name |salary |
_ _ __ ___
1 | 10 | A | 100$ |
_ _ __ ___
2 | 25 | A | 105$ |
_ _ __ ___
3 | 15 | A | 110$ |
_ _ __ ___
4 | 22 | A | 150$ |
我想做一个想象中的年龄组,比如10-20岁的人是第二组,20-30岁的人是第一组
最后,我们希望按薪资说明排序,该说明将年龄保留在组内,但按薪资说明排序,而不是按年龄排序
结果会是这样的
id| age | name |salary |
_ _ __ ___
4 | 22 | A | 150$ |
_ _ __ ___
2 | 25 | A | 105$ |
_ _ __ ___
3 | 19 | A | 110$ |
_ _ __ ___
1 | 15 | A | 100$ |
在这里,您可以看到id3的薪水比id2高,但id2取决于组1,因此它将在id2之前出现。您可以使用
大小写表达式:
order by
case
when age > 20 and age <= 30 then 1
when age > 10 and age <= 20 then 2
..................................
end,
salary desc
您可以使用大小写
表达式执行此操作:
order by
case
when age > 20 and age <= 30 then 1
when age > 10 and age <= 20 then 2
..................................
end,
salary desc
欢迎来到SO。请看:欢迎来到SO。请参阅:
> id | age | name | salary
> -: | --: | :--- | -----:
> 4 | 22 | A | 150
> 2 | 25 | A | 105
> 3 | 19 | A | 110
> 1 | 15 | A | 100