MENU

sql 语句简单总结—下

• March 10, 2019 • Read: 63 • python

MySQL查询命令小结。

准备工作:

首先,创建一个数据库数据表用来查询。

create database query_demo charset=utf8;

使用数据库:

use query_demo;

创建students表:

create table students(
    id int unsigned primary key auto_increment not null,
    name varchar(20) default '',
    age tinyint unsigned default 0,
    height decimal(5,2),
    gender enum('男','女','中性','保密') default '保密',
    cls_id int unsigned default 0,
    is_delete bit default 0
);

创建classes表:

create table classes (
    id int unsigned auto_increment primary key not null,
    name varchar(30) not null
);

如下:

mysql> create database query_demo  charset=utf8;
Query OK, 1 row affected (0.00 sec)

mysql> use query_demo;
Database changed
mysql> create table students(
    ->     id int unsigned primary key auto_increment not null,
    ->     name varchar(20) default '',
    ->     age tinyint unsigned default 0,
    ->     height decimal(5,2),
    ->     gender enum('男','女','中性','保密') default '保密',
    ->     cls_id int unsigned default 0,
    ->     is_delete bit default 0
    -> );
Query OK, 0 rows affected (0.02 sec)

mysql> create table classes (
    ->     id int unsigned auto_increment primary key not null,
    ->     name varchar(30) not null
    -> );
Query OK, 0 rows affected (0.01 sec)

准备数据:

向students表中写入:

insert into students values
(0,'小明',18,180.00,2,1,0),
(0,'小月月',18,180.00,2,2,1),
(0,'彭于晏',29,185.00,1,1,0),
(0,'刘德华',59,175.00,1,2,1),
(0,'黄蓉',38,160.00,2,1,0),
(0,'凤姐',28,150.00,4,2,1),
(0,'王祖贤',18,172.00,2,1,1),
(0,'周杰伦',36,NULL,1,1,0),
(0,'程坤',27,181.00,1,2,0),
(0,'刘亦菲',25,166.00,2,2,0),
(0,'金星',33,162.00,3,3,1),
(0,'静香',12,180.00,2,4,0),
(0,'郭靖',12,170.00,1,4,0),
(0,'周杰',34,176.00,2,5,0);

向classes表中写入:

insert into classes values (0, "python_01期"), (0, "python_02期");

如下:

mysql> insert into students values
    -> (0,'小明',18,180.00,2,1,0),
    -> (0,'小月月',18,180.00,2,2,1),
    -> (0,'彭于晏',29,185.00,1,1,0),
    -> (0,'刘德华',59,175.00,1,2,1),
    -> (0,'黄蓉',38,160.00,2,1,0),
    -> (0,'凤姐',28,150.00,4,2,1),
    -> (0,'王祖贤',18,172.00,2,1,1),
    -> (0,'周杰伦',36,NULL,1,1,0),
    -> (0,'程坤',27,181.00,1,2,0),
    -> (0,'刘亦菲',25,166.00,2,2,0),
    -> (0,'金星',33,162.00,3,3,1),
    -> (0,'静香',12,180.00,2,4,0),
    -> (0,'郭靖',12,170.00,1,4,0),
    -> (0,'周杰',34,176.00,2,5,0);
Query OK, 14 rows affected (0.00 sec)
Records: 14  Duplicates: 0  Warnings: 0

mysql> insert into classes values (0, "python_01期"), (0, "python_02期");
Query OK, 2 rows affected (0.01 sec)
Records: 2  Duplicates: 0  Warnings: 0

基础查询:

查询所有字段:

select * from 表名;

例如:select * from students;

mysql> select *from students;
+----+-----------+------+--------+--------+--------+-----------+
| id | name      | age  | height | gender | cls_id | is_delete |
+----+-----------+------+--------+--------+--------+-----------+
|  1 | 小明      |   18 | 180.00 | 女     |      1 |           |
|  2 | 小月月    |   18 | 180.00 | 女     |      2 |          |
|  3 | 彭于晏    |   29 | 185.00 | 男     |      1 |           |
|  4 | 刘德华    |   59 | 175.00 | 男     |      2 |          |
|  5 | 黄蓉      |   38 | 160.00 | 女     |      1 |           |
|  6 | 凤姐      |   28 | 150.00 | 保密   |      2 |          |
|  7 | 王祖贤    |   18 | 172.00 | 女     |      1 |          |
|  8 | 周杰伦    |   36 |   NULL | 男     |      1 |           |
|  9 | 程坤      |   27 | 181.00 | 男     |      2 |           |
| 10 | 刘亦菲    |   25 | 166.00 | 女     |      2 |           |
| 11 | 金星      |   33 | 162.00 | 中性   |      3 |          |
| 12 | 静香      |   12 | 180.00 | 女     |      4 |           |
| 13 | 郭靖      |   12 | 170.00 | 男     |      4 |           |
| 14 | 周杰      |   34 | 176.00 | 女     |      5 |           |
+----+-----------+------+--------+--------+--------+-----------+
14 rows in set (0.00 sec)

查询指定字段

select 列1,列2,... from 表名;

例如:select name, age from students;

mysql> select name, age from students;
+-----------+------+
| name      | age  |
+-----------+------+
| 小明      |   18 |
| 小月月    |   18 |
| 彭于晏    |   29 |
| 刘德华    |   59 |
| 黄蓉      |   38 |
| 凤姐      |   28 |
| 王祖贤    |   18 |
| 周杰伦    |   36 |
| 程坤      |   27 |
| 刘亦菲    |   25 |
| 金星      |   33 |
| 静香      |   12 |
| 郭靖      |   12 |
| 周杰      |   34 |
+-----------+------+
14 rows in set (0.00 sec)

还有一种方式:

select 表名.字段 .... from 表名;

例如:select students.name, students.age from students;

mysql> select students.name, students.age from students;
+-----------+------+
| name      | age  |
+-----------+------+
| 小明      |   18 |
| 小月月    |   18 |
| 彭于晏    |   29 |
| 刘德华    |   59 |
| 黄蓉      |   38 |
| 凤姐      |   28 |
| 王祖贤    |   18 |
| 周杰伦    |   36 |
| 程坤      |   27 |
| 刘亦菲    |   25 |
| 金星      |   33 |
| 静香      |   12 |
| 郭靖      |   12 |
| 周杰      |   34 |
+-----------+------+
14 rows in set (0.00 sec)

使用 as 给字段起别名

select 字段 as 名字.... from 表名;

例如:select name as 姓名, age as 年龄 from students;

mysql> select name as 姓名, age as 年龄 from students;
+-----------+--------+
| 姓名      | 年龄   |
+-----------+--------+
| 小明      |     18 |
| 小月月    |     18 |
| 彭于晏    |     29 |
| 刘德华    |     59 |
| 黄蓉      |     38 |
| 凤姐      |     28 |
| 王祖贤    |     18 |
| 周杰伦    |     36 |
| 程坤      |     27 |
| 刘亦菲    |     25 |
| 金星      |     33 |
| 静香      |     12 |
| 郭靖      |     12 |
| 周杰      |     34 |
+-----------+--------+
14 rows in set (0.00 sec)

可以通过 as 给表起别名

select 别名.字段 .... from 表名 as 别名;

例如:select s.name, s.age from students as s;

mysql> select s.name, s.age from students as s;
+-----------+------+
| name      | age  |
+-----------+------+
| 小明      |   18 |
| 小月月    |   18 |
| 彭于晏    |   29 |
| 刘德华    |   59 |
| 黄蓉      |   38 |
| 凤姐      |   28 |
| 王祖贤    |   18 |
| 周杰伦    |   36 |
| 程坤      |   27 |
| 刘亦菲    |   25 |
| 金星      |   33 |
| 静香      |   12 |
| 郭靖      |   12 |
| 周杰      |   34 |
+-----------+------+
14 rows in set (0.00 sec)

消除重复行:

在select后面列前使用distinct可以消除重复的行

select distinct 列1,... from 表名;

例如:select distinct gender from students;

mysql> select distinct gender from students;
+--------+
| gender |
+--------+
| 女     |
| 男     |
| 保密   |
| 中性   |
+--------+
4 rows in set (0.00 sec)

条件查询:

使用where子句对表中的数据筛选,结果为true的行会出现在结果集中。

语法:

select * from 表名 where 条件;

例如:select id,name,gender from students where age>18;

mysql> select id,name,gender from students where age>18;
+----+-----------+--------+
| id | name      | gender |
+----+-----------+--------+
|  3 | 彭于晏    | 男     |
|  4 | 刘德华    | 男     |
|  5 | 黄蓉      | 女     |
|  6 | 凤姐      | 保密   |
|  8 | 周杰伦    | 男     |
|  9 | 程坤      | 男     |
| 10 | 刘亦菲    | 女     |
| 11 | 金星      | 中性   |
| 14 | 周杰      | 女     |
+----+-----------+--------+
9 rows in set (0.00 sec)

where后面支持多种运算符,进行条件的处理

比较运算符

等于: =
大于: >
大于等于: >=
小于: <
小于等于: <=
不等于: != 或 <>

例如:查询编号大于3的学生:select * from students where id>3;

mysql> select * from students where id >3;
+----+-----------+------+--------+--------+--------+-----------+
| id | name      | age  | height | gender | cls_id | is_delete |
+----+-----------+------+--------+--------+--------+-----------+
|  4 | 刘德华    |   59 | 175.00 | 男     |      2 |          |
|  5 | 黄蓉      |   38 | 160.00 | 女     |      1 |           |
|  6 | 凤姐      |   28 | 150.00 | 保密   |      2 |          |
|  7 | 王祖贤    |   18 | 172.00 | 女     |      1 |          |
|  8 | 周杰伦    |   36 |   NULL | 男     |      1 |           |
|  9 | 程坤      |   27 | 181.00 | 男     |      2 |           |
| 10 | 刘亦菲    |   25 | 166.00 | 女     |      2 |           |
| 11 | 金星      |   33 | 162.00 | 中性   |      3 |          |
| 12 | 静香      |   12 | 180.00 | 女     |      4 |           |
| 13 | 郭靖      |   12 | 170.00 | 男     |      4 |           |
| 14 | 周杰      |   34 | 176.00 | 女     |      5 |           |
+----+-----------+------+--------+--------+--------+-----------+
11 rows in set (0.00 sec)

逻辑运算符

and
or
not

例如:查询编号大于3的女同学:select * from students where id >3 and gender=2;

mysql> select * from students where id >3 and gender =2;
+----+-----------+------+--------+--------+--------+-----------+
| id | name      | age  | height | gender | cls_id | is_delete |
+----+-----------+------+--------+--------+--------+-----------+
|  5 | 黄蓉      |   38 | 160.00 | 女     |      1 |           |
|  7 | 王祖贤    |   18 | 172.00 | 女     |      1 |          |
| 10 | 刘亦菲    |   25 | 166.00 | 女     |      2 |           |
| 12 | 静香      |   12 | 180.00 | 女     |      4 |           |
| 14 | 周杰      |   34 | 176.00 | 女     |      5 |           |
+----+-----------+------+--------+--------+--------+-----------+
5 rows in set (0.00 sec)

模糊查询

like
%表示任意多(大于等于0)个任意字符
_表示一个任意字符

例如:查询姓黄或叫靖的学生:select * from students where name like "黄%" or name like "%靖";

mysql> select * from students where name like '黄%' or name like '%靖';
+----+--------+------+--------+--------+--------+-----------+
| id | name   | age  | height | gender | cls_id | is_delete |
+----+--------+------+--------+--------+--------+-----------+
|  5 | 黄蓉   |   38 | 160.00 | 女     |      1 |           |
| 13 | 郭靖   |   12 | 170.00 | 男     |      4 |           |
+----+--------+------+--------+--------+--------+-----------+
2 rows in set (0.00 sec)

或者 rlike 正则表达式:

例如: 查询以周开始、伦结尾的姓名: select * from students where name rlike '^周.*伦$';

mysql> select * from students where name rlike '^周.*伦$';
+----+-----------+------+--------+--------+--------+-----------+
| id | name      | age  | height | gender | cls_id | is_delete |
+----+-----------+------+--------+--------+--------+-----------+
|  8 | 周杰伦    |   36 |   NULL | 男     |      1 |           |
+----+-----------+------+--------+--------+--------+-----------+
1 row in set (0.00 sec)

范围查询

in表示在一个非连续的范围内:

例如:in (12, 18, 34)表示在一个非连续的范围内:select * from students where age in(12,18,34);

mysql> select * from students where age in(12,18,34);
+----+-----------+------+--------+--------+--------+-----------+
| id | name      | age  | height | gender | cls_id | is_delete |
+----+-----------+------+--------+--------+--------+-----------+
|  1 | 小明      |   18 | 180.00 | 女     |      1 |           |
|  2 | 小月月    |   18 | 180.00 | 女     |      2 |          |
|  7 | 王祖贤    |   18 | 172.00 | 女     |      1 |          |
| 12 | 静香      |   12 | 180.00 | 女     |      4 |           |
| 13 | 郭靖      |   12 | 170.00 | 男     |      4 |           |
| 14 | 周杰      |   34 | 176.00 | 女     |      5 |           |
+----+-----------+------+--------+--------+--------+-----------+
6 rows in set (0.00 sec)

not in 不非连续的范围之内:
例如:年龄不是12,18、34岁之间的信息:select * from students where age not in(12,18,34);

mysql> select * from students where age not in(12,18,34);
+----+-----------+------+--------+--------+--------+-----------+
| id | name      | age  | height | gender | cls_id | is_delete |
+----+-----------+------+--------+--------+--------+-----------+
|  3 | 彭于晏    |   29 | 185.00 | 男     |      1 |           |
|  4 | 刘德华    |   59 | 175.00 | 男     |      2 |          |
|  5 | 黄蓉      |   38 | 160.00 | 女     |      1 |           |
|  6 | 凤姐      |   28 | 150.00 | 保密   |      2 |          |
|  8 | 周杰伦    |   36 |   NULL | 男     |      1 |           |
|  9 | 程坤      |   27 | 181.00 | 男     |      2 |           |
| 10 | 刘亦菲    |   25 | 166.00 | 女     |      2 |           |
| 11 | 金星      |   33 | 162.00 | 中性   |      3 |          |
+----+-----------+------+--------+--------+--------+-----------+
8 rows in set (0.00 sec)

between ... and ...表示在一个连续的范围内:

例如:查询编号是3至8的男生:

select * from students where (id between 3 and 8) and gender =1;

mysql> select * from students where (id between 3 and 8) and gender =1;
+----+-----------+------+--------+--------+--------+-----------+
| id | name      | age  | height | gender | cls_id | is_delete |
+----+-----------+------+--------+--------+--------+-----------+
|  3 | 彭于晏    |   29 | 185.00 | 男     |      1 |           |
|  4 | 刘德华    |   59 | 175.00 | 男     |      2 |          |
|  8 | 周杰伦    |   36 |   NULL | 男     |      1 |           |
+----+-----------+------+--------+--------+--------+-----------+
3 rows in set (0.00 sec)

not between ... and ...表示不在一个连续的范围内:

例如:查询年龄不在在18到34之间的的信息:

select * from students where age not between 18 and 34;
mysql> select * from students where age not between 18 and 34;
+----+-----------+------+--------+--------+--------+-----------+
| id | name      | age  | height | gender | cls_id | is_delete |
+----+-----------+------+--------+--------+--------+-----------+
|  4 | 刘德华    |   59 | 175.00 | 男     |      2 |          |
|  5 | 黄蓉      |   38 | 160.00 | 女     |      1 |           |
|  8 | 周杰伦    |   36 |   NULL | 男     |      1 |           |
| 12 | 静香      |   12 | 180.00 | 女     |      4 |           |
| 13 | 郭靖      |   12 | 170.00 | 男     |      4 |           |
+----+-----------+------+--------+--------+--------+-----------+
5 rows in set (0.00 sec)

空判断

注意:null与''是不同的
判空is null

例如:查询填写了身高的男生:select * from students where height in not null and gender=1;

排序:

order by 字段
asc从小到大排列,即升序
desc从大到小排序,即降序

例如:查询年龄在18到34岁之间的男性,按照年龄从小到到排序:select * from students where gender=1 and ( age between 18 and 34 ) order by age asc;

mysql> select * from students where gender=1 and (age between 18 and 35 ) order by age asc;
+----+-----------+------+--------+--------+--------+-----------+
| id | name      | age  | height | gender | cls_id | is_delete |
+----+-----------+------+--------+--------+--------+-----------+
|  9 | 程坤      |   27 | 181.00 | 男     |      2 |           |
|  3 | 彭于晏    |   29 | 185.00 | 男     |      1 |           |
+----+-----------+------+--------+--------+--------+-----------+
2 rows in set (0.00 sec)

order by 多个字段:

例如:查询年龄在18到34岁之间的女性,身高从高到矮排序, 如果身高相同的情况下按照年龄从小到大排序:select * from students where gender=2 and (age between 18 and 34) order by height desc, age asc;

mysql> select * from students where gender=2 and (age between 18 and 34) order by height desc,age asc;
+----+-----------+------+--------+--------+--------+-----------+
| id | name      | age  | height | gender | cls_id | is_delete |
+----+-----------+------+--------+--------+--------+-----------+
|  1 | 小明      |   18 | 180.00 | 女     |      1 |           |
|  2 | 小月月    |   18 | 180.00 | 女     |      2 |          |
| 14 | 周杰      |   34 | 176.00 | 女     |      5 |           |
|  7 | 王祖贤    |   18 | 172.00 | 女     |      1 |          |
| 10 | 刘亦菲    |   25 | 166.00 | 女     |      2 |           |
+----+-----------+------+--------+--------+--------+-----------+
5 rows in set (0.00 sec)

聚合函数:

1):总数count():count()表示计算总行数,括号中写星与列名,结果是相同的

例如:查询男性有多少人:

mysql> select * from students where gender =1;
+----+-----------+------+--------+--------+--------+-----------+
| id | name      | age  | height | gender | cls_id | is_delete |
+----+-----------+------+--------+--------+--------+-----------+
|  3 | 彭于晏    |   29 | 185.00 | 男     |      1 |           |
|  4 | 刘德华    |   59 | 175.00 | 男     |      2 |          |
|  8 | 周杰伦    |   36 |   NULL | 男     |      1 |           |
|  9 | 程坤      |   27 | 181.00 | 男     |      2 |           |
| 13 | 郭靖      |   12 | 170.00 | 男     |      4 |           |
+----+-----------+------+--------+--------+--------+-----------+
5 rows in set (0.00 sec)

mysql> select count(*)  from students where gender =1;
+----------+
| count(*) |
+----------+
|        5 |
+----------+
1 row in set (0.00 sec)

mysql> select count(*) as 男性人数  from students where gender =1;
+--------------+
| 男性人数     |
+--------------+
|            5 |
+--------------+
1 row in set (0.00 sec)

2):最大值:max(列)表示求此列的最大值:

例如:查询女性的最高身高:

select max(height) as 女性身高 from students where gender=1;
mysql> select max(height) as  女性身高 from students  where gender = 1;
+--------------+
| 女性身高     |
+--------------+
|       185.00 |
+--------------+
1 row in set (0.00 sec)

3):最小值:min(列)表示求此列的最小值:

例如:查询未删除的学生最小编号:

select min(id) from students where is_delete =1;

mysql> select min(id) from students where is_delete =1;
+---------+
| min(id) |
+---------+
|       2 |
+---------+
1 row in set (0.00 sec)

4):求和:sum(列)表示求此列的和:

例如:查询男生的平均年龄:

select sum(age)/count(*) from students where gender=2;

mysql> select sum(age)/count(*) from students where gender=1;
+-------------------+
| sum(age)/count(*) |
+-------------------+
|           32.6000 |
+-------------------+
1 row in set (0.00 sec)

注:四舍五入 round(123.23 , 1) 保留1位小数;

例如:查询男生的平均年龄,保留一位小数:

select round(sum(age)/count(*),1) from students where gender =1;
mysql> select round(sum(age)/count(*),1) from students where gender =1;
+----------------------------+
| round(sum(age)/count(*),1) |
+----------------------------+
|                       32.6 |
+----------------------------+
1 row in set (0.00 sec)

5):平均值:avg(列)表示求此列的平均值:

例如:查询未删除女生的编号平均值:

select avg(id) from students where is_delete=1 and gender=2;
mysql> select avg(id) from students where is_delete=1 and gender=2;
+---------+
| avg(id) |
+---------+
|  4.5000 |
+---------+
1 row in set (0.00 sec)

分组:

group by

group by的含义:将查询结果按照1个或多个字段进行分组,字段值相同的为一组;
group by可用于单个字段分组,也可用于多个字段分组。

例如:计算每种性别的人数:select gender,count(gender) from students group by gender;

mysql> select gender,count(gender) from students group by gender;
+--------+---------------+
| gender | count(gender) |
+--------+---------------+
| 男     |             5 |
| 女     |             7 |
| 中性   |             1 |
| 保密   |             1 |
+--------+---------------+
4 rows in set (0.00 sec)

group by + group_concat()

group_concat(字段名)可以作为一个输出字段来使用,
表示分组之后,根据分组结果,使用group_concat()来放置每一组的某字段的值的集合。

例如:查询同种性别中的姓名:select gender as 性别,group_concat(name) as 姓名 from students group by gender;

mysql> select gender as 性别,group_concat(name) as 姓名 from students group by gender;
+--------+-----------------------------------------------------------+
| 性别   | 姓名                                                      |
+--------+-----------------------------------------------------------+
| 男     | 彭于晏,刘德华,周杰伦,程坤,郭靖                            |
| 女     | 小明,小月月,黄蓉,王祖贤,刘亦菲,静香,周杰                  |
| 中性   | 金星                                                      |
| 保密   | 凤姐                                                      |
+--------+-----------------------------------------------------------+
4 rows in set (0.00 sec)

group_concat()可以跟多个参数,例如:

mysql> select gender,group_concat(name, " 年龄: ", age) from students group by gender;
+--------+----------------------------------------------------------------------------------------------------------------------------------------+
| gender | group_concat(name, " 年龄: ", age)                                                                                                     |
+--------+----------------------------------------------------------------------------------------------------------------------------------------+
| 男     | 彭于晏 年龄: 29,刘德华 年龄: 59,周杰伦 年龄: 36,程坤 年龄: 27,郭靖 年龄: 12                                                            |
| 女     | 小明 年龄: 18,小月月 年龄: 18,黄蓉 年龄: 38,王祖贤 年龄: 18,刘亦菲 年龄: 25,静香 年龄: 12,周杰 年龄: 34                                |
| 中性   | 金星 年龄: 33                                                                                                                          |
| 保密   | 凤姐 年龄: 28                                                                                                                          |
+--------+----------------------------------------------------------------------------------------------------------------------------------------+
4 rows in set (0.00 sec)

group by + 集合函数

通过group_concat()的启发,我们既然可以统计出每个分组的某字段的值的集合,那么我们也可以通过集合函数来对这个值的集合做一些操作。

例如:分别统计性别为男/女的人年龄平均值:

select gender as 性别,avg(age) as 平均值 from students where gender=1 or gender =2  group by gender;
mysql> select gender as 性别,avg(age) as 平均值 from students where gender=1 or gender =2  group by gender;
+--------+-----------+
| 性别   | 平均值    |
+--------+-----------+
| 男     |   32.6000 |
| 女     |   23.2857 |
+--------+-----------+
2 rows in set (0.01 sec)

group by + having

having 条件表达式:用来分组查询后指定一些条件来输出查询结果;
having作用和where一样,但having只能用于group by。

此处需要注意having和where地区别。

例如:查询每种性别中的人数多于2个的性别的信息:select gender,group_concat(name) from students group by gender having count(gender)>2;

mysql> select gender,group_concat(name) from students group by gender having count(gender)>2;
+--------+-----------------------------------------------------------+
| gender | group_concat(name)                                        |
+--------+-----------------------------------------------------------+
| 男     | 彭于晏,刘德华,周杰伦,程坤,郭靖                            |
| 女     | 小明,小月月,黄蓉,王祖贤,刘亦菲,静香,周杰                  |
+--------+-----------------------------------------------------------+
2 rows in set (0.00 sec)

group by + with rollup

with rollup的作用是:在最后新增一行,来记录当前列里所有记录的总和。

例如:select gender,count(*) from students group by gender with rollup;

mysql> select gender,count(*) from students group by gender with rollup;
+--------+----------+
| gender | count(*) |
+--------+----------+
| 男     |        5 |
| 女     |        7 |
| 中性   |        1 |
| 保密   |        1 |
| NULL   |       14 |
+--------+----------+
5 rows in set (0.00 sec)

分页:

语法:

select * from 表名 limit start,count;

例如:每页显示2个,显示第6页的信息, 按照年龄从小到大排序:

select * from students order by age asc limit 10,2 ;
mysql> select * from students order by age asc limit 10,2 ;
+----+-----------+------+--------+--------+--------+-----------+
| id | name      | age  | height | gender | cls_id | is_delete |
+----+-----------+------+--------+--------+--------+-----------+
| 14 | 周杰      |   34 | 176.00 | 女     |      5 |           |
|  8 | 周杰伦    |   36 |   NULL | 男     |      1 |           |
+----+-----------+------+--------+--------+--------+-----------+
2 rows in set (0.00 sec)

格式:求第n页数据,每页显示m个:select * from students where 条件 limit (n-1)*m,m;

连接查询:

当查询结果的列来源于多张表时,需要将多张表连接成一个大的数据集,再选择合适的列返回;mysql支持三种类型的连接查询,分别为:

内连接查询:查询的结果为两个表匹配到的数据

1

右连接查询:查询的结果为两个表匹配到的数据,右表特有的数据,对于左表中不存在的数据使用null填充

2

左连接查询:查询的结果为两个表匹配到的数据,左表特有的数据,对于右表中不存在的数据使用null填充

3

语法:select * from 表1 inner或left或right join 表2 on 表1.列 = 表2.列;

例1:使用内连接查询班级表与学生表:select * from students inner join classes on students.cls_id=classes.id;

mysql> select * from students inner join classes on students.cls_id=classes.id;
+----+-----------+------+--------+--------+--------+-----------+----+--------------+
| id | name      | age  | height | gender | cls_id | is_delete | id | name         |
+----+-----------+------+--------+--------+--------+-----------+----+--------------+
|  1 | 小明      |   18 | 180.00 | 女     |      1 |           |  1 | python_01期  |
|  2 | 小月月    |   18 | 180.00 | 女     |      2 |          |  2 | python_02期  |
|  3 | 彭于晏    |   29 | 185.00 | 男     |      1 |           |  1 | python_01期  |
|  4 | 刘德华    |   59 | 175.00 | 男     |      2 |          |  2 | python_02期  |
|  5 | 黄蓉      |   38 | 160.00 | 女     |      1 |           |  1 | python_01期  |
|  6 | 凤姐      |   28 | 150.00 | 保密   |      2 |          |  2 | python_02期  |
|  7 | 王祖贤    |   18 | 172.00 | 女     |      1 |          |  1 | python_01期  |
|  8 | 周杰伦    |   36 |   NULL | 男     |      1 |           |  1 | python_01期  |
|  9 | 程坤      |   27 | 181.00 | 男     |      2 |           |  2 | python_02期  |
| 10 | 刘亦菲    |   25 | 166.00 | 女     |      2 |           |  2 | python_02期  |
+----+-----------+------+--------+--------+--------+-----------+----+--------------+
10 rows in set (0.00 sec)

例2:使用左连接查询班级表与学生表:select * from students as s left join classes as c on s.cls_id=c.id;

mysql> select * from students as s left join classes as c on s.cls_id=c.id;
+----+-----------+------+--------+--------+--------+-----------+------+--------------+
| id | name      | age  | height | gender | cls_id | is_delete | id   | name         |
+----+-----------+------+--------+--------+--------+-----------+------+--------------+
|  1 | 小明      |   18 | 180.00 | 女     |      1 |           |    1 | python_01期  |
|  3 | 彭于晏    |   29 | 185.00 | 男     |      1 |           |    1 | python_01期  |
|  5 | 黄蓉      |   38 | 160.00 | 女     |      1 |           |    1 | python_01期  |
|  7 | 王祖贤    |   18 | 172.00 | 女     |      1 |          |    1 | python_01期  |
|  8 | 周杰伦    |   36 |   NULL | 男     |      1 |           |    1 | python_01期  |
|  2 | 小月月    |   18 | 180.00 | 女     |      2 |          |    2 | python_02期  |
|  4 | 刘德华    |   59 | 175.00 | 男     |      2 |          |    2 | python_02期  |
|  6 | 凤姐      |   28 | 150.00 | 保密   |      2 |          |    2 | python_02期  |
|  9 | 程坤      |   27 | 181.00 | 男     |      2 |           |    2 | python_02期  |
| 10 | 刘亦菲    |   25 | 166.00 | 女     |      2 |           |    2 | python_02期  |
| 11 | 金星      |   33 | 162.00 | 中性   |      3 |          | NULL | NULL         |
| 12 | 静香      |   12 | 180.00 | 女     |      4 |           | NULL | NULL         |
| 13 | 郭靖      |   12 | 170.00 | 男     |      4 |           | NULL | NULL         |
| 14 | 周杰      |   34 | 176.00 | 女     |      5 |           | NULL | NULL         |
+----+-----------+------+--------+--------+--------+-----------+------+--------------+
14 rows in set (0.00 sec)

例3:使用右连接查询班级表与学生表:select * from students as s right join classes as c on s.cls_id=c.id;

mysql> select * from students as s right join classes as c on s.cls_id=c.id;
+------+-----------+------+--------+--------+--------+-----------+----+--------------+
| id   | name      | age  | height | gender | cls_id | is_delete | id | name         |
+------+-----------+------+--------+--------+--------+-----------+----+--------------+
|    1 | 小明      |   18 | 180.00 | 女     |      1 |           |  1 | python_01期  |
|    2 | 小月月    |   18 | 180.00 | 女     |      2 |          |  2 | python_02期  |
|    3 | 彭于晏    |   29 | 185.00 | 男     |      1 |           |  1 | python_01期  |
|    4 | 刘德华    |   59 | 175.00 | 男     |      2 |          |  2 | python_02期  |
|    5 | 黄蓉      |   38 | 160.00 | 女     |      1 |           |  1 | python_01期  |
|    6 | 凤姐      |   28 | 150.00 | 保密   |      2 |          |  2 | python_02期  |
|    7 | 王祖贤    |   18 | 172.00 | 女     |      1 |          |  1 | python_01期  |
|    8 | 周杰伦    |   36 |   NULL | 男     |      1 |           |  1 | python_01期  |
|    9 | 程坤      |   27 | 181.00 | 男     |      2 |           |  2 | python_02期  |
|   10 | 刘亦菲    |   25 | 166.00 | 女     |      2 |           |  2 | python_02期  |
+------+-----------+------+--------+--------+--------+-----------+----+--------------+
10 rows in set (0.00 sec)

例4:查询学生姓名及班级名称:select s.name,c.name from students as s inner join classes as c on s.cls_id=c.id;

mysql> select s.name,c.name from students as s inner join classes as c on s.cls_id=c.id;
+-----------+--------------+
| name      | name         |
+-----------+--------------+
| 小明      | python_01期  |
| 小月月    | python_02期  |
| 彭于晏    | python_01期  |
| 刘德华    | python_02期  |
| 黄蓉      | python_01期  |
| 凤姐      | python_02期  |
| 王祖贤    | python_01期  |
| 周杰伦    | python_01期  |
| 程坤      | python_02期  |
| 刘亦菲    | python_02期  |
+-----------+--------------+
10 rows in set (0.00 sec)

自关联:

没有找到合适的数据;
不再重负造轮子:
https://www.jianshu.com/p/d0d1b430edfb

子查询:

在一个 select 语句中,嵌入了另外一个 select 语句, 那么被嵌入的 select 语句称之为子查询语句

主查询

主要查询的对象,第一条 select 语句

主查询和子查询的关系

子查询是嵌入到主查询中
子查询是辅助主查询的,要么充当条件,要么充当数据源
子查询是可以独立存在的语句,是一条完整的 select 语句

子查询分类

标量子查询: 子查询返回的结果是一个数据(一行一列)
列子查询: 返回的结果是一列(一列多行)
行子查询: 返回的结果是一行(一行多列)

标量子查询

查询大于平均年龄的学生

语句:select * from students where age>(select avg(age) from students);

mysql> select * from students where age>(select avg(age) from students);
+----+-----------+------+--------+--------+--------+-----------+
| id | name      | age  | height | gender | cls_id | is_delete |
+----+-----------+------+--------+--------+--------+-----------+
|  3 | 彭于晏    |   29 | 185.00 | 男     |      1 |           |
|  4 | 刘德华    |   59 | 175.00 | 男     |      2 |          |
|  5 | 黄蓉      |   38 | 160.00 | 女     |      1 |           |
|  6 | 凤姐      |   28 | 150.00 | 保密   |      2 |          |
|  8 | 周杰伦    |   36 |   NULL | 男     |      1 |           |
| 11 | 金星      |   33 | 162.00 | 中性   |      3 |          |
| 14 | 周杰      |   34 | 176.00 | 女     |      5 |           |
+----+-----------+------+--------+--------+--------+-----------+
7 rows in set (0.00 sec)

列级子查询
查询还有学生在班的所有班级名字

语句:select name from classes where id in (select cls_id from students where cls_id is not null);

mysql> select name from classes where id in (select cls_id from students where cls_id is not null);
+--------------+
| name         |
+--------------+
| python_01期  |
| python_02期  |
+--------------+
2 rows in set (0.00 sec)

行级子查询:
查找班级年龄最大,身高最高的学生:select * from students where (height,age) = (select max(height),max(age) from students);

Tags: python
Archives QR Code Tip
QR Code for this page
Tipping QR Code
Leave a Comment