mysql查询语句select多种用法
一、使用select按关键字排序
select按关键字排序的语法格式
SELECT column1(字段1),column2,… FROM 表名 ORDER BY column1,column2,… ASC|DESC;
1)结合使用ORDER BY 语句来实现排序
2)排序可以针对一个或多个字段
3)ASC:升序排列,mysql默认的排列方式,没有声明排列方式的情况下系统默认升序排列
4)DESC:降序排列
实例操作:
- 查看系统表默认排序方式
mysql> select * from stu; //查询数据库中stu表中的系统默认按主键升序排列方式
+----+---------+--------+-------+
| id | name | score | hoby | //可以看到这个表由4个字段和六条记录组成
+----+---------+--------+-------+
| 1 | tianxia | 78.00 | 2 |
| 2 | diyi | 89.00 | 1 |
| 3 | wode | 55.00 | 1 |
| 4 | tade | 57.00 | 3 |
| 5 | nida | 66.00 | 1 |
| 6 | liushou | 55.00 | 3 |
+----+---------+-------+------+
6 rows in set (0.00 sec)
- 将表改为基于id降序排列的方式
mysql> select * from stu order by id desc;
+----+---------+--------+-------+
| id | name | score | hoby |
+----+---------+--------+-------+
| 6 | liushou | 55.00 | 3 |
| 5 | nida | 66.00 | 1 |
| 4 | tade | 57.00 | 3 |
| 3 | wode | 55.00 | 1 |
| 2 | diyi | 89.00 | 1 |
| 1 | tianxia | 78.00 | 2 |
+----+---------+-------+------+
6 rows in set (0.00 sec)
- 将表改为先基于score字段降序排列,在基于hoby字段排列。
mysql> select * from stu order by score desc,hoby desc;
+----+---------+-------+------+
| id | name | score | hoby |
+----+---------+-------+------+
| 2 | diyi | 89.00 | 1 |
| 1 | tianxia | 78.00 | 2 |
| 5 | nida | 66.00 | 1 |
| 4 | tade | 57.00 | 3 |
| 6 | liushou | 55.00 | 3 |
| 3 | wode | 55.00 | 1 |
+----+---------+-------+------+
6 rows in set (0.00 sec)
二、使用select对结果进行分组s
select对结果进行分组的语法格式
SELECT column,函数 FROM 表名 WHERE 条件 GROUP BY 字段;
1)使用GROUP BY 语句实现分组
2)可以结合函数一起使用,通常结合聚合函数count (字段名)
3)可以按一个或多个字段对结果经行分组
实例操作
- 统计表中hoby字段每个组的中人的个数
mysql> select count(name),hoby from stu where hoby >= 1 group by hoby;
+-------------+------+
| count(name) | hoby |
+-------------+------+
| 3 | 1 |
| 1 | 2 |
| 2 | 3 |
+-------------+------+
3 rows in set (0.00 sec)
- 统计表中hoby字段每个组的中人的个数,并将统计到的count(name)按降序排列。
mysql> select count(name),hoby from stu group by hoby order by count(name) desc;
+-------------+------+
| count(name) | hoby |
+-------------+------+
| 3 | 1 |
| 2 | 3 |
| 1 | 2 |
+-------------+------+
3 rows in set (0.00 sec)
三、使用select查询限制条目结果
使用select查询限制条目结果的语法结构
SELECT column1,column2,… FROM 表名 LIMIT [offset,] number;
1)[offset,]位置偏移量,从0开始。中括号在语法结构中是可以省略的。
2)number返回记录行的最大数目。
3)从offset位置行开始,往下查询number行记录。
4)LIMIT语句用来限制读取记录。
实例操作
- 查寻表的前三行
mysql> select * from stu limit 0,3;
+----+---------+-------+------+
| id | name | score | hoby |
+----+---------+-------+------+
| 1 | tianxia | 78.00 | 2 |
| 2 | diyi | 89.00 | 1 |
| 3 | wode | 55.00 | 1 |
+----+---------+-------+------+
3 rows in set (0.00 sec)
- 查询表的4到6行
mysql> select * from stu limit 3,3;
+----+---------+-------+------+
| id | name | score | hoby |
+----+---------+-------+------+
| 4 | tade | 57.00 | 3 |
| 5 | nida | 66.00 | 1 |
| 6 | liushou | 55.00 | 3 |
+----+---------+-------+------+
3 rows in set (0.00 sec)
四、使用select设置别名
使用select设置别名的语法结构
SELECT column(字段名) AS column(字段名) FROM 表名
- 可以对字段和表设置别名
- 使用AS语句设置别名时,关键字AS可以省略
- 设置别名时,保证不能与库中的其他表或字段名冲突
4) AS除了可以设置别名还可以作为连接语句
实例操作
- 为字段设置别名统计字段记录次数
mysql> select count(name) as a from stu;
±–+
| a |
±–+
| 6 |
±–+
1 row in set (0.00 sec) - 为表设置别名查看前三行的id
mysql> select c.id from stu as c limit 3;
±—+
| id |
±—+
| 1 |
| 2 |
| 3 |
±—+
3 rows in set (0.00 sec) - 使用AS作为连接语句
将stu表中的数据通过AS连接语句将数据导入到新建表中
mysql> create table xuexi as select * from stu where score >= 80;
Query OK, 1 row affected (0.00 sec)
Records: 1 Duplicates: 0 Warnings: 0
mysql> desc stu;
+-------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+----------------+
| id | int(5) | NO | PRI | NULL | auto_increment |
| name | char(15) | NO | | NULL | |
| score | decimal(5,2) | YES | | NULL | |
| hoby | int(5) | YES | | NULL | |
+-------+--------------+------+-----+---------+----------------+
4 rows in set (0.00 sec)
mysql> desc xuexi;
+-------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+-------+
| id | int(4) | YES | | 0 | |
| name | char(15) | NO | | NULL | |
| score | decimal(5,2) | YES | | NULL | |
| hoby | int(5) | YES | | NULL | |
+-------+--------------+------+-----+---------+-------+
4 rows in set (0.00 sec)
AS实际是定义了新建表的表结构和连接表一摸一样的表结构,并将连接表的数据导入到新建表中
但是新建表的一些约束类型是没有和连接表一样定义的,例如,key主键、Default默认值、Extra拓展属性等。
五、使用select常用的通配符
常用的通配符常用语法结构
SELECT column(字段名) FROM 表名 WHERE column LIKE ‘_%’
- % 表示零个、一个或者多个
- _表示单个字符
1)通常用于模糊查询,不清楚具体,只知道大概时可以使用
2)用于表示字段中部分字符
3)通常结合like一起使用,配合WHERE完成查询
实例操作
- 通配符’%'的用法
查询表stu表中name字段中以w开头的记录
mysql> select * from stu where name like 'w%';
+----+------+-------+------+
| id | name | score | hoby |
+----+------+-------+------+
| 3 | wode | 55.00 | 1 |
+----+------+-------+------+
1 row in set (0.00 sec)
- 通配符’_'的用法
查询stu表中name字段中前面由两个任意字符并且以de结尾的记录
mysql> select * from stu where name like '__de';
+----+------+-------+------+
| id | name | score | hoby |
+----+------+-------+------+
| 3 | wode | 55.00 | 1 |
| 4 | tade | 57.00 | 3 |
+----+------+-------+------+
2 rows in set (0.00 sec)
- 通配符’_%'的结合用法
查询stu表中name字段中前面由一个任意字符中间有一个i字符并且以de结尾的记录
mysql> select * from stu where name like '_i%';
+----+---------+-------+------+
| id | name | score | hoby |
+----+---------+-------+------+
| 1 | tianxia | 78.00 | 2 |
| 2 | diyi | 89.00 | 1 |
| 5 | nida | 66.00 | 1 |
| 6 | liushou | 55.00 | 3 |
+----+---------+-------+------+
4 rows in set (0.00 sec)
六、使用select的子查询
使用select子查询的常用语法结构
SELECT column(字段)FROM 表名 WHERE column IN (SELECT column FROM 表名 WHERE column IN ( …));
1)子查询也称作内查询或者嵌套查询
2)先于主查询被执行,其结果将作为外层主查询的条件
3)在增删改查中都可以使用子查询
4)支持多层嵌套
5) 读取方式是从里一层层往外读取
6)IN语句是用来判断某个值是否在给定的结果集中
7)子查询中的字段类型一定要和主查询的字段类型一样
8) 当使用结果集作为子查询的表时,不能直接使用,要为结果集建立别名
实例操作
- 基于表子查询
查询zhi表
mysql> select * from zhi;
±-----+
| id |
±-----+
| 1 |
| 3 |
| 4 |
| 5 |
±-----+
4 rows in set (0.01 sec)
查询stu表
mysql> select * from stu;
+----+---------+-------+------+
| id | name | score | hoby |
+----+---------+-------+------+
| 1 | tianxia | 78.00 | 2 |
| 2 | diyi | 89.00 | 1 |
| 3 | wode | 55.00 | 1 |
| 4 | tade | 57.00 | 3 |
| 5 | nida | 66.00 | 1 |
| 6 | liushou | 55.00 | 3 |
+----+---------+-------+------+
6 rows in set (0.01 sec)
将zhi表的id值作为stu表的id查询条件查询数据库记录
mysql> select * from stu where id in (select id from zhi);
+----+---------+-------+------+
| id | name | score | hoby |
+----+---------+-------+------+
| 1 | tianxia | 78.00 | 2 |
| 3 | wode | 55.00 | 1 |
| 4 | tade | 57.00 | 3 |
| 5 | nida | 66.00 | 1 |
+----+---------+-------+------+
4 rows in set (0.00 sec)
- 基于结果集子查询
查询stu表中的两个字段的记录
mysql> select id,score from stu;
±—±------+
| id | score |
±—±------+
| 1 | 78.00 |
| 2 | 89.00 |
| 3 | 55.00 |
| 4 | 57.00 |
| 5 | 66.00 |
| 6 | 55.00 |
±—±------+
6 rows in set (0.00 sec)
将查询到的结果集作为子查询语句时要设置别名才行
mysql> select a.id from (select id,name from stu) a;
±—+
| id |
±—+
| 1 |
| 2 |
| 3 |
| 4 |
| 5 |
| 6 |
±—+
6 rows in set (0.00 sec)
七、使用select支持的正则表达式查询
使用select支持的正则表达式查询的语法结构
SELECT column… FROM 表名 WHERE column REGEXP ‘正则表达式’
1)根据指定的匹配模式匹配记录中符合要求的特殊字符
2)使用REGEXP关键字指定匹配模式
实例操作
- 查询name字段以w开头的记录
mysql> select * from stu where name regexp '^w';
+----+------+-------+------+
| id | name | score | hoby |
+----+------+-------+------+
| 3 | wode | 55.00 | 1 |
+----+------+-------+------+
1 row in set (0.00 sec)
- 查询name字段以de结尾的记录
mysql> select * from stu where name regexp 'de$';
+----+------+-------+------+
| id | name | score | hoby |
+----+------+-------+------+
| 3 | wode | 55.00 | 1 |
| 4 | tade | 57.00 | 3 |
+----+------+-------+------+
2 rows in set (0.00 sec)
- 查询hoby字段为1 的记录
mysql> select * from stu where hoby regexp '1';
+----+------+-------+------+
| id | name | score | hoby |
+----+------+-------+------+
| 2 | diyi | 89.00 | 1 |
| 3 | wode | 55.00 | 1 |
| 5 | nida | 66.00 | 1 |
+----+------+-------+------+
3 rows in set (0.00 sec)
八、使用select判断NULL的值
NUll值得介绍
1)表示确缺失的值
2)表示空的对象是占用空间的
3)与数字0或者空白(spaces)是不同的
4)使用IS NULL或IS NOT NULL 进行判断
5)NUll值和空值得区别
- 空值长度为0, 不占用空间;NULL值得长度为NULL,占用空间
- IS NULL无法判断空值
- 空值使用“=”或者“<>(不等于)”来处理
- COUNT() 计算时,NULL会忽略不会加入统计,空值会加入计算
实例操作
mysql> select * from stu;
+----+---------+-------+------+------+
| id | name | score | hoby | dizi |
+----+---------+-------+------+------+
| 1 | tianxia | 78.00 | 2 | nj |
| 2 | diyi | 89.00 | 1 | nj |
| 3 | wode | 55.00 | 1 | NULL |
| 4 | tade | 57.00 | 3 | NULL |
| 5 | nida | 66.00 | 1 | nj |
| 6 | liushou | 55.00 | 3 | |
+----+---------+-------+------+------+
6 rows in set (0.00 sec)
- 查询为NULL值得记录
mysql> select * from stu where dizi is null;
+----+------+-------+------+------+
| id | name | score | hoby | dizi |
+----+------+-------+------+------+
| 3 | wode | 55.00 | 1 | NULL |
| 4 | tade | 57.00 | 3 | NULL |
+----+------+-------+------+------+
2 rows in set (0.00 sec)
- 查询为NOT NULL 值得记录
mysql> select * from stu where dizi is not null;
+----+---------+-------+------+------+
| id | name | score | hoby | dizi |
+----+---------+-------+------+------+
| 1 | tianxia | 78.00 | 2 | nj |
| 2 | diyi | 89.00 | 1 | nj |
| 5 | nida | 66.00 | 1 | nj |
| 6 | liushou | 55.00 | 3 | |
+----+---------+-------+------+------+
4 rows in set (0.00 sec)
- NULL不会被加入统计
mysql> select count(dizi) from stu;
±------------+
| count(dizi) |
±------------+
| 4 |
±------------+
1 row in set (0.00 sec)
原本有六条记录,但是只统计了四条,因为COUNT()统计计算时,NULL会忽略不会加入统计,空值会加入计算