mysql数据库排序规则
MySQL数据库排序规则:
一、MySQL中定义的排序规则:
1.ASC:升序
2.DESC: 降序
二、排序规则在MySQL中的应用:
1、按照数值大小排序:
(1)升序:select * from xxx order by 字段名 asc;
(2)降序:select * from xxx order by 字段名 desc;
2、按照字母或字符的首字母的字典顺序排序:
(1)升序:select * from xxx order by 字段名 asc;
(2)降序:select * from xxx order by 字段名 desc;
三、排序可以选择多个字段作为标准:
(1)select * from xxx order by 字段1 asc, 字段2 asc ;
(2)select * from xxx order by 字段1 desc, 字段2 asc ;
(3)select * from xxx order by 字段1 asc, 字段2 desc ;
(4)select * from xxx order by 字段1 desc, 字段2 desc ; 此处 order by 后面的asc/desc为单个字段的排序规则。 mysql group by order by
四、排序中的其它参数:
1、limit
limit可以限定返回的结果的记录的数量,格式为limit m,n,其中m表示从第m+1条记录开始显示;n表示显示的记录数目。
2、offset
offset相当于limit m,n中的m,表示从结果集中第m+1条记录开始显示;
3、group by
group by跟在where子句之后,可将查询结果依据指定的列标题(即group by 后指定的字段)形成分组,是统计报表查询常用的手法。结果集中每一组数据只显示一次,可以在 group by 后用 having 来筛选某组数据。
4、having
having可以在 group by 条件下使用,它跟在group by句子之后,格式与where的格式相同,用于设定分组条件,进行二次筛选。
五、特殊用法:
1、多字段排序:select * from xxx order by 字段1 asc, 字段2 asc;
2、限定结果集的记录的数量:select * from xxx order by 字段1 asc, 字段2 asc limit 10;
3、限定从结果集中第m+1条记录开始显示:select * from xxx order by 字段1 asc, 字段2 asc limit 10 offset 4;
4、根据指定字段分组,且只显示满足条件的分组:select 字段1, sum(字段2) as total from table_name group by 字段1 having total>8000;