mysql时间排序分页_Mysql排序和分页(orderbylimit)及存在的坑
mysql时间排序分页_Mysql排序和分页(orderbylimit)及存在
的坑
周记 开学第一周排序查询(order by)
电商中:我们想查看今天所有成交的订单,按照交易额从⾼到低排序,此时我们可以使⽤数据库中的排序功能来完成。
排序语法:
select 字段名 from 表名 order by 字段1 [asc|desc],字段2 [asc|desc];
需要排序的字段跟在order by之后;
asc|desc表⽰排序的规则,asc:升序,desc:降序,默认为asc;
⽀持多个字段进⾏排序,多字段排序之间⽤逗号隔开。
单字段排序
mysql> create table test2(a int,b varchar(10));
Query OK, 0 rows affected (0.01 sec)
mysql> insert into test2 values (10,'jack'),(8,'tom'),(5,'ready'),(100,'javacode');
Query OK, 4 rows affected (0.00 sec)
Records: 4 Duplicates: 0 Warnings: 0
mysql> select * from test2;
+------+----------+
| a | b |
+------+----------+
| 10 | jack |
| 8 | tom |
| 5 | ready |
显示器突然黑屏| 100 | javacode |
+------+----------+
4 rows in set (0.00 sec)
mysql> select * from test2 order by a asc;
+------+----------+
| a | b |
+------+----------+
| 5 | ready |
| 8 | tom |
| 10 | jack |
| 100 | javacode |
+------+----------+
4 rows in set (0.00 sec)
mysql> select * from test2 order by a desc;
+------+----------+
| a | b |
+------+----------+
专利许可合同
| 100 | javacode |
| 10 | jack |
| 8 | tom |
| 5 | ready |
+------+----------+
4 rows in set (0.00 sec)
mysql> select * from test2 order by a;
+------+----------+
| a | b |
+------+----------+
| 5 | ready |
| 8 | tom |
| 10 | jack |
| 100 | javacode |
+------+----------+
4 rows in set (0.00 sec)
多字段排序
⽐如学⽣表,先按学⽣年龄降序,年龄相同时,再按学号升序,如下:
mysql> create table stu(id int not null comment '学号' primary key,age tinyint not null comment '年龄',name varchar(16) comment '姓名');
Query OK, 0 rows affected (0.01 sec)
mysql> insert into stu (id,age,name) values (1001,18,'路⼈甲Java'),(1005,20,'刘德华'),(1003,18,'张学友'),(1004,20,'张国荣'), (1010,19,'梁朝伟');
Query OK, 5 rows affected (0.00 sec)
Records: 5 Duplicates: 0 Warnings: 0
mysql> select * from stu;
+------+-----+---------------+
| id | age | name |
+------+-----+---------------+
| 1001 | 18 | 路⼈甲Java |
| 1003 | 18 | 张学友 |
| 1004 | 20 | 张国荣 |
| 1005 | 20 | 刘德华 |
| 1010 | 19 | 梁朝伟 |
+------+-----+---------------+
5 rows in set (0.00 sec)
mysql> select * from stu order by age desc,id asc;
+------+-----+---------------+
| id | age | name |
+------+-----+---------------+
| 1004 | 20 | 张国荣 |
| 1005 | 20 | 刘德华 |
| 1010 | 19 | 梁朝伟 |
lol盲僧| 1001 | 18 | 路⼈甲Java |
| 1003 | 18 | 张学友 |
+------+-----+---------------+
5 rows in set (0.00 sec)
按别名排序
mysql> select * from stu;
+------+-----+---------------+
| id | age | name |
+------+-----+---------------+
| 1001 | 18 | 路⼈甲Java |
| 1003 | 18 | 张学友 |
| 1004 | 20 | 张国荣 |
| 1005 | 20 | 刘德华 |
| 1010 | 19 | 梁朝伟 |
+------+-----+---------------+
5 rows in set (0.00 sec)
mysql> select age '年龄',id as '学号' from stu order by 年龄 asc,学号 desc; +--------+--------+
| 年龄 | 学号 |
+--------+--------+
游记作文500字| 18 | 1003 |
| 18 | 1001 |
| 19 | 1010 |
| 20 | 1005 |
| 20 | 1004 |
+--------+--------+
按函数排序
有学⽣表(id:编号,birth:出⽣⽇期,name:姓名),如下:
mysql> drop table if exists student;
大日如来佛Query OK, 0 rows affected (0.01 sec)
mysql> CREATE TABLE student (
-> id int(11) NOT NULL COMMENT '学号',
-> birth date NOT NULL COMMENT '出⽣⽇期',
-> name varchar(16) DEFAULT NULL COMMENT '姓名',
-> PRIMARY KEY (id)
-> );
Query OK, 0 rows affected (0.01 sec)
mysql> insert into student (id,birth,name) values (1001,'1990-10-10','路⼈甲Java'),(1005,'1960-03-01','刘德华'), (1003,'1960-08-16','张学友'),(1004,'1968-07-01','张国荣'),(1010,'1962-05-16','梁朝伟');
Query OK, 5 rows affected (0.00 sec)
Records: 5 Duplicates: 0 Warnings: 0
mysql>
mysql> SELECT * FROM student;
+------+------------+---------------+
| id | birth | name |
+------+------------+---------------+
| 1001 | 1990-10-10 | 路⼈甲Java |
| 1003 | 1960-08-16 | 张学友 |
| 1004 | 1968-07-01 | 张国荣 |
| 1005 | 1960-03-01 | 刘德华 |
| 1010 | 1962-05-16 | 梁朝伟 |
+------+------------+---------------+
5 rows in set (0.00 sec)
需求:按照出⽣年份升序、编号升序,查询出编号、出⽣⽇期、出⽣年份、姓名,2种写法如下:
mysql> SELECT id 编号,birth 出⽣⽇期,year(birth) 出⽣年份,name 姓名 from student ORDER BY year(birth) asc,id asc; +--------+--------------+--------------+---------------+
| 编号 | 出⽣⽇期 | 出⽣年份 | 姓名 |
+--------+--------------+--------------+---------------+
| 1003 | 1960-08-16 | 1960 | 张学友 |
| 1005 | 1960-03-01 | 1960 | 刘德华 |
| 1010 | 1962-05-16 | 1962 | 梁朝伟 |
| 1004 | 1968-07-01 | 1968 | 张国荣 |
| 1001 | 1990-10-10 | 1990 | 路⼈甲Java |
+--------+--------------+--------------+---------------+
5 rows in set (0.00 sec)
mysql> SELECT id 编号,birth 出⽣⽇期,year(birth) 出⽣年份,name 姓名 from student ORDER BY 出⽣年份 asc,id asc;
+--------+--------------+--------------+---------------+
| 编号 | 出⽣⽇期 | 出⽣年份 | 姓名 |
+--------+--------------+--------------+---------------+
| 1003 | 1960-08-16 | 1960 | 张学友 |
| 1005 | 1960-03-01 | 1960 | 刘德华 |
| 1010 | 1962-05-16 | 1962 | 梁朝伟 |
| 1004 | 1968-07-01 | 1968 | 张国荣 |
| 1001 | 1990-10-10 | 1990 | 路⼈甲Java |
+--------+--------------+--------------+---------------+
5 rows in set (0.00 sec)
说明:
year函数:属于⽇期函数,可以获取对应⽇期中的年份。
上⾯使⽤了2种⽅式排序,第⼀种是在order by中使⽤了函数,第⼆种是使⽤了别名排序。
where之后进⾏排序
有订单数据如下:
mysql> drop table if exists t_order;
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> create table t_order(
-> id int not null auto_increment comment '订单编号',
-> price decimal(10,2) not null default 0 comment '订单⾦额',
-> primary key(id)
-
> )comment '订单表';

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。