1.order by 排序
mysql> select * from xin order by id;
+------+----------+
| id | name |
+------+----------+
| 1 | lilie |
| 2 | tianjin |
| 3 | shanghai |
+------+----------+
2.按照升序排列
mysql> select * from xin order by id asc;
+------+----------+
| id | name |
+------+----------+
| 1 | lilie |
| 2 | tianjin |
| 3 | shanghai |
+------+----------+
3.按降序排列
mysql> select * from xin order by id desc;
+------+----------+
| id | name |
+------+----------+
| 3 | shanghai |
| 2 | tianjin |
| 1 | lilie |
+------+----------+
---------------------------------------------
create table student(
Son int(10) NOT NULL COMMENT ‘学号‘,
Sname varchar(16) NOT NULL COMMENT ‘姓名‘,
Ssex char(2) NOT NULL COMMENT ‘性别‘,
Sage tinyint(2) NOT NULL default ‘0‘ COMMENT ‘学生年龄‘,
Sdept varchar(16) default NULL COMMENT ‘学生所在系别‘,
PRIMARY KEY (Son),
key index_Sname(Sname)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=latin1;
create table course(
Cno int(10) NOT NULL COMMENT ‘课程号‘,
Cname varchar(64) NOT NULL COMMENT ‘课程名‘,
Ccredit tinyint(10) NOT NULL COMMENT ‘学分‘,
PRIMARY KEY (Cno)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=latin1;
create table sc (
SCid int(12) NOT NULL auto_increment COMMENT ‘主键‘,
Cno int(10) NOT NULL COMMENT ‘课程号‘,
Sno int(10) NOT NULL COMMENT ‘学号‘,
Grade tinyint(2) NOT NULL COMMENT ‘学生成绩‘,
PRIMARY KEY (SCid)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
insert into student values (0001,‘宏志‘,‘男‘,30,‘计算机网络‘);
insert into student values (0002,‘王硕‘,‘男‘,30,‘computer application‘);
insert into student values (0003,‘oldboy‘,‘男‘,28,‘物流管理‘);
insert into student values(0004,‘脉动‘,‘男‘,29,‘computer application‘);
insert into student values(0005,‘oldgirl‘,‘女‘,26,‘计算机科学与技术‘);
insert into student values(0006,‘莹莹‘,‘女‘,22,‘护士‘);
insert into course values(1001,‘Linux中高级运维‘,3);
insert into course values(1002,‘Linux高级架构师‘,5);
insert into course values(1003,‘MySQL高级Dba‘,4);
insert into course values(1004,‘Python运维开发‘,4);
insert into course values(1005,‘Java web开发‘,3);
insert into sc (Sno,Cno,Grade) values (0001,1001,4);
insert into sc (Sno,Cno,Grade) values (0001,1002,3);
insert into sc (Sno,Cno,Grade) values (0001,1003,1);
insert into sc (Sno,Cno,Grade) values (0001,1004,6);
insert into sc (Sno,Cno,Grade) values (0002,1001,3);
insert into sc (Sno,Cno,Grade) values (0002,1002,2);
insert into sc (Sno,Cno,Grade) values (0002,1003,2);
insert into sc (Sno,Cno,Grade) values (0002,1004,8);
insert into sc (Sno,Cno,Grade) values (0003,1001,4);
insert into sc (Sno,Cno,Grade) values (0003,1002,4);
insert into sc (Sno,Cno,Grade) values (0003,1003,2);
insert into sc (Sno,Cno,Grade) values (0003,1001,8);
insert into sc (Sno,Cno,Grade) values (0004,1001,1);
insert into sc (Sno,Cno,Grade) values (0004,1002,1);
insert into sc (Sno,Cno,Grade) values (0004,1003,2);
insert into sc (Sno,Cno,Grade) values (0004,1004,3);
insert into sc (Sno,Cno,Grade) values (0005,1001,5);
insert into sc (Sno,Cno,Grade) values (0005,1002,3);
insert into sc (Sno,Cno,Grade) values (0005,1003,2);
insert into sc (Sno,Cno,Grade) values (0005,1004,9);
--------------------------------------------
4.多表查询
mysql> select student.Sno,student.Sname,course.Cname,sc.Grade from student,course,sc where student.Sno=sc.Sno and course.Cno=sc.Cno;
+-----+---------+----------------------+-------+
| Sno | Sname | Cname | Grade |
+-----+---------+----------------------+-------+
| 1 | 宏志 | Linux中高级运维 | 4 |
| 2 | 王硕 | Linux中高级运维 | 3 |
| 3 | oldboy | Linux中高级运维 | 4 |
| 3 | oldboy | Linux中高级运维 | 8 |
| 4 | 脉动 | Linux中高级运维 | 1 |
| 5 | oldgirl | Linux中高级运维 | 5 |
| 1 | 宏志 | Linux高级架构师 | 3 |
| 2 | 王硕 | Linux高级架构师 | 2 |
| 3 | oldboy | Linux高级架构师 | 4 |
| 4 | 脉动 | Linux高级架构师 | 1 |
| 5 | oldgirl | Linux高级架构师 | 3 |
| 1 | 宏志 | MySQL高级Dba | 1 |
| 2 | 王硕 | MySQL高级Dba | 2 |
| 3 | oldboy | MySQL高级Dba | 2 |
| 4 | 脉动 | MySQL高级Dba | 2 |
| 5 | oldgirl | MySQL高级Dba | 2 |
| 1 | 宏志 | Python运维开发 | 6 |
| 2 | 王硕 | Python运维开发 | 8 |
| 4 | 脉动 | Python运维开发 | 3 |
| 5 | oldgirl | Python运维开发 | 9 |
+-----+---------+----------------------+-------+
20 rows in set (0.06 sec)
5.多表查询按照Sno排序
mysql> select student.Sno,student.Sname,course.Cname,sc.Grade from student,course,sc where student.Sno=sc.Sno and course.Cno=sc.Cno order by Sno;
+-----+---------+----------------------+-------+
| Sno | Sname | Cname | Grade |
+-----+---------+----------------------+-------+
| 1 | 宏志 | Linux中高级运维 | 4 |
| 1 | 宏志 | Python运维开发 | 6 |
| 1 | 宏志 | MySQL高级Dba | 1 |
| 1 | 宏志 | Linux高级架构师 | 3 |
| 2 | 王硕 | Linux中高级运维 | 3 |
| 2 | 王硕 | Python运维开发 | 8 |
| 2 | 王硕 | MySQL高级Dba | 2 |
| 2 | 王硕 | Linux高级架构师 | 2 |
| 3 | oldboy | Linux高级架构师 | 4 |
| 3 | oldboy | Linux中高级运维 | 8 |
| 3 | oldboy | Linux中高级运维 | 4 |
| 3 | oldboy | MySQL高级Dba | 2 |
| 4 | 脉动 | MySQL高级Dba | 2 |
| 4 | 脉动 | Linux高级架构师 | 1 |
| 4 | 脉动 | Linux中高级运维 | 1 |
| 4 | 脉动 | Python运维开发 | 3 |
| 5 | oldgirl | Python运维开发 | 9 |
| 5 | oldgirl | MySQL高级Dba | 2 |
| 5 | oldgirl | Linux高级架构师 | 3 |
| 5 | oldgirl | Linux中高级运维 | 5 |
+-----+---------+----------------------+-------+
20 rows in set (0.01 sec)
6.多表查询按照倒叙排列
mysql> select student.Sno,student.Sname,course.Cname,sc.Grade from student,course,sc where student.Sno=sc.Sno and course.Cno=sc.Cno order by student.Sno DESC;
+-----+---------+----------------------+-------+
| Sno | Sname | Cname | Grade |
+-----+---------+----------------------+-------+
| 5 | oldgirl | Python运维开发 | 9 |
| 5 | oldgirl | MySQL高级Dba | 2 |
| 5 | oldgirl | Linux高级架构师 | 3 |
| 5 | oldgirl | Linux中高级运维 | 5 |
| 4 | 脉动 | MySQL高级Dba | 2 |
| 4 | 脉动 | Linux高级架构师 | 1 |
| 4 | 脉动 | Linux中高级运维 | 1 |
| 4 | 脉动 | Python运维开发 | 3 |
| 3 | oldboy | Linux高级架构师 | 4 |
| 3 | oldboy | Linux中高级运维 | 8 |
| 3 | oldboy | Linux中高级运维 | 4 |
| 3 | oldboy | MySQL高级Dba | 2 |
| 2 | 王硕 | Linux中高级运维 | 3 |
| 2 | 王硕 | Python运维开发 | 8 |
| 2 | 王硕 | MySQL高级Dba | 2 |
| 2 | 王硕 | Linux高级架构师 | 2 |
| 1 | 宏志 | Linux中高级运维 | 4 |
| 1 | 宏志 | Python运维开发 | 6 |
| 1 | 宏志 | MySQL高级Dba | 1 |
| 1 | 宏志 | Linux高级架构师 | 3 |
+-----+---------+----------------------+-------+
20 rows in set (0.00 sec)
7.多表查询按照正序排列
mysql> select student.Sno,student.Sname,course.Cname,sc.Grade from student,course,sc where student.Sno=sc.Sno and course.Cno=sc.Cno order by student.Sno ASC;
+-----+---------+----------------------+-------+
| Sno | Sname | Cname | Grade |
+-----+---------+----------------------+-------+
| 1 | 宏志 | Linux中高级运维 | 4 |
| 1 | 宏志 | Python运维开发 | 6 |
| 1 | 宏志 | MySQL高级Dba | 1 |
| 1 | 宏志 | Linux高级架构师 | 3 |
| 2 | 王硕 | Linux中高级运维 | 3 |
| 2 | 王硕 | Python运维开发 | 8 |
| 2 | 王硕 | MySQL高级Dba | 2 |
| 2 | 王硕 | Linux高级架构师 | 2 |
| 3 | oldboy | Linux高级架构师 | 4 |
| 3 | oldboy | Linux中高级运维 | 8 |
| 3 | oldboy | Linux中高级运维 | 4 |
| 3 | oldboy | MySQL高级Dba | 2 |
| 4 | 脉动 | MySQL高级Dba | 2 |
| 4 | 脉动 | Linux高级架构师 | 1 |
| 4 | 脉动 | Linux中高级运维 | 1 |
| 4 | 脉动 | Python运维开发 | 3 |
| 5 | oldgirl | Python运维开发 | 9 |
| 5 | oldgirl | MySQL高级Dba | 2 |
| 5 | oldgirl | Linux高级架构师 | 3 |
| 5 | oldgirl | Linux中高级运维 | 5 |
+-----+---------+----------------------+-------+
20 rows in set (0.00 sec)