在数据库的使用中排序和过滤也是常常的操做mysql
排序检索数据,关键字ordergit
##1.按照某个列名排序github
普通排序 mysql> select * from user; +----+--------+-----+-----------------------+-----+ | id | name | age | address | sex | +----+--------+-----+-----------------------+-----+ | 1 | 张三 | 20 | 北京海底市南区 | 1 | | 2 | 李四 | 22 | 北京海底市南区 | 1 | | 3 | 赵芸 | 32 | 上海市徐汇区 | 2 | | 4 | 王丽 | 31 | 广州厦门 | 2 | +----+--------+-----+-----------------------+-----+ 4 rows in set (0.00 sec) 按照列名name排序是什么样呢? mysql> select * from user order by name; +----+--------+-----+-----------------------+-----+ | id | name | age | address | sex | +----+--------+-----+-----------------------+-----+ | 1 | 张三 | 20 | 北京海底市南区 | 1 | | 2 | 李四 | 22 | 北京海底市南区 | 1 | | 4 | 王丽 | 31 | 广州厦门 | 2 | | 3 | 赵芸 | 32 | 上海市徐汇区 | 2 | +----+--------+-----+-----------------------+-----+ 4 rows in set (0.00 sec)
##2.按照多个列名排序sql
mysql> select * from user order by name, age; +----+--------+-----+-----------------------+-----+ | id | name | age | address | sex | +----+--------+-----+-----------------------+-----+ | 1 | 张三 | 20 | 北京海底市南区 | 1 | | 2 | 李四 | 22 | 北京海底市南区 | 1 | | 4 | 王丽 | 31 | 广州厦门 | 2 | | 3 | 赵芸 | 32 | 上海市徐汇区 | 2 | +----+--------+-----+-----------------------+-----+ 4 rows in set (0.00 sec)
##3.指定排序方向,默认为字母(a-z),升序数据库
使用关键字desc,能够改成降序排列 mysql> select * from user order by name desc; +----+--------+-----+-----------------------+-----+ | id | name | age | address | sex | +----+--------+-----+-----------------------+-----+ | 3 | 赵芸 | 32 | 上海市徐汇区 | 2 | | 4 | 王丽 | 31 | 广州厦门 | 2 | | 2 | 李四 | 22 | 北京海底市南区 | 1 | | 1 | 张三 | 20 | 北京海底市南区 | 1 | +----+--------+-----+-----------------------+-----+ 4 rows in set (0.00 sec)
##4.和limit配合使用,限制检索数据数量网站
mysql> select * from user order by name limit 3; +----+--------+-----+-----------------------+-----+ | id | name | age | address | sex | +----+--------+-----+-----------------------+-----+ | 1 | 张三 | 20 | 北京海底市南区 | 1 | | 2 | 李四 | 22 | 北京海底市南区 | 1 | | 4 | 王丽 | 31 | 广州厦门 | 2 | +----+--------+-----+-----------------------+-----+ 3 rows in set (0.00 sec)
数据过滤,关键字wherecode
##1.检索某一条记录排序
mysql> select * from user where id = 2; +----+--------+-----+-----------------------+-----+ | id | name | age | address | sex | +----+--------+-----+-----------------------+-----+ | 2 | 李四 | 22 | 北京海底市南区 | 1 | +----+--------+-----+-----------------------+-----+ 1 row in set (0.00 sec) 和order by 配合使用 mysql> select * from user where id <4 order by name limit 3; +----+--------+-----+-----------------------+-----+ | id | name | age | address | sex | +----+--------+-----+-----------------------+-----+ | 1 | 张三 | 20 | 北京海底市南区 | 1 | | 2 | 李四 | 22 | 北京海底市南区 | 1 | | 3 | 赵芸 | 32 | 上海市徐汇区 | 2 | +----+--------+-----+-----------------------+-----+ 3 rows in set (0.00 sec) 关于where子句的位置: 在同时使用where和order by子句时候, 咱们应该让order by位于where 子句以后。
##2.范围检索--betweenget
mysql> select * from user where id between 2 and 4; +----+--------+-----+-----------------------+-----+ | id | name | age | address | sex | +----+--------+-----+-----------------------+-----+ | 2 | 李四 | 22 | 北京海底市南区 | 1 | | 3 | 赵芸 | 32 | 上海市徐汇区 | 2 | | 4 | 王丽 | 31 | 广州厦门 | 2 | +----+--------+-----+-----------------------+-----+ 3 rows in set (0.00 sec)
##3.过滤--组合whereit
mysql> select * from user where id >1 and id < 4; +----+--------+-----+-----------------------+-----+ | id | name | age | address | sex | +----+--------+-----+-----------------------+-----+ | 2 | 李四 | 22 | 北京海底市南区 | 1 | | 3 | 赵芸 | 32 | 上海市徐汇区 | 2 | +----+--------+-----+-----------------------+-----+ 2 rows in set (0.00 sec)
mysql> select * from user where id <2 or id >=3; +----+--------+-----+-----------------------+-----+ | id | name | age | address | sex | +----+--------+-----+-----------------------+-----+ | 1 | 张三 | 20 | 北京海底市南区 | 1 | | 3 | 赵芸 | 32 | 上海市徐汇区 | 2 | | 4 | 王丽 | 31 | 广州厦门 | 2 | +----+--------+-----+-----------------------+-----+ 3 rows in set (0.00 sec)
##5.数据过滤--in 操做符
in操做符能够用于指定操做范围,范围内每一个条件均可以进行匹配。 mysql> select * from user where name in ("张三","李四"); +----+--------+-----+-----------------------+-----+ | id | name | age | address | sex | +----+--------+-----+-----------------------+-----+ | 1 | 张三 | 20 | 北京海底市南区 | 1 | | 2 | 李四 | 22 | 北京海底市南区 | 1 | +----+--------+-----+-----------------------+-----+ 2 rows in set (0.00 sec) in操做符的优点: 1. 使用长的合法选项清单时候, in操做符比较直观。 2. in操做符计算的次序比较好管理 3. in操做符通常比or操做符效率快 4. in操做符能够包括其余select语句,可以更加动态的建立where子句
##6.数据过滤--not操做符
not操做符只有一个特色, 就是否认它后面的任何条件。 mysql支持not对in, between, exists子句取反。 mysql> select * from user where name not in ("张三","李四"); +----+--------+-----+--------------------+-----+ | id | name | age | address | sex | +----+--------+-----+--------------------+-----+ | 3 | 赵芸 | 32 | 上海市徐汇区 | 2 | | 4 | 王丽 | 31 | 广州厦门 | 2 | +----+--------+-----+--------------------+-----+ 2 rows in set (0.00 sec)