Home > Database > Mysql Tutorial > body text

数据库 语句 总结

WBOY
Release: 2016-06-07 17:37:36
Original
973 people have browsed it

数据库 语句 总结 问题:表#lastTable,列为:[provinceId],[allTypeSum],里面有几十行数据,我想选出全部,要排序,但是provinceId=40的不需要排,就是说我想把除了allTypeSum=40以外的全部行按allTypeSum值desc排序,再把id=40行放到结果的最后面,然后输出 解决方

数据库 语句 总结

  问题:表#lastTable,列为:[provinceId],[allTypeSum],里面有几十行数据,我想选出全部,要排序,但是provinceId=40的不需要排,就是说我想把除了allTypeSum=40以外的全部行按allTypeSum值desc排序,再把id=40行放到结果的最后面,然后输出

  解决方法一:

select * from ( select top 100 * from #lastTable where provinceId!=40 order by allTypeSum desc --注意这里必须要top这样的语句 )T union all select * from #lastTable where provinceId=40

其实这里差不多就是用了一个临时表,但是注意多重select和top

  解决方法二:

tempColunm from #lastTable order by tempColunm asc, allTypeSum desc

其实这方法用到了case语句,就相当于给表新增加了一个列, provinceId=40的行的新加列值是1,其他都是0,然后再按两个排序,果真新加了一个列结果如下:

----------------------------------------------------------------------------------------

学到:.case语句新增临时列的强大

----------------------------------------------------------------------------------------

posted on

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!