Mysql 三: 数据库简单的查询

一、简介

查询的基本语法

select * from 表名;

from 关键字后面写 表名,表示数据来源于哪张表

select 后面写表中的列名,如果是 * 表示在结果中显示表中所有列。

在 select 后面的列名部分,可以使用 as 为列起别名,这个别名出现在结果集中。

如果要查询多个列,之间使用逗号分隔。

二、消除重复行

在 select 后面列前使用 distinct 可以消除重复的行(重复行是指所有字段都相同的行,有多个字段就要全部判断)

例如 :

select distinct brithday from students;

+---------------------+

| brithday |

+---------------------+

| 1994-02-14 00:00:00 |

+---------------------+

1 row in set (0.00 sec)

三、条件

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

where 前面的是 【原始集】,后面的是 【条件】,筛选得到的结果就是 【结果集】

select * from 表名 where 条件;

条件里面可以写哪些东西呢?

1.比较运算符

等于 =

大于 >

大于等于 >=

小于 <

小于等于 <=

不等于 != 或 <>

例如:

查询编号大于3的学生

select * from students where id>3;

查询编号不大于4的科目

select * from subjects where id<=4;

.....

2.逻辑运算符

and

or

not

例如 :

查询编号大于3的女同学

select * from students where id>3 and gender=0;

查询编号小于4或者没被 删除的学生

select * from students where id<4 or isDelete=0;

3.模糊查询

kill

% 表示任意多个 任意字符

_ 表示一个任意字符

例如 :

查询姓黄的 学生

select * from students where name like '黄%';

查询姓黄并且名只有一个字的 学生

select * from students where name like '黄_';

查询姓黄或者 名为 靖 的学生

select * from students where name like '黄%' or name like '_靖';

4.范围查询

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

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

例如:

查询编号是1或3或8的学生

select * from students where id in (3,5,8);

查询编号是3到 8的学生

select * from students where id between 3 and 8;

5.空判断

注意 null 与 '' 是不同的

判空 is null

判非空 is not null

例如 :

查询没有填写地址的学生

select * from students where hometwon is null;

查询填写了地址的学生

select * from students where hometwon is not null;

5.优先级

小括号 > not > 比较运算符 > 逻辑运算符

and 比 or 先运算,如果 同时出现并希望 先运算 or ,需要结合 ()使用。

四、聚合 (聚合就是对现有的多行数据进行统计,使你只能看到统计的结果)

为了快速得到统计数据,提供了5个聚合函数

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

例如 :

查询学生总数

select count(*) from students;

+----------+

| count(*) |

+----------+

| 5 |

+----------+

1 row in set (0.00 sec)

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

例如 :

查询女生编号的最大值

select max(id) from students where gender=1;

+---------+

| max(id) |

+---------+

| 5 |

+---------+

1 row in set (0.00 sec)

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

例如 :

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

select min(id) from students where isDelete=0;

+---------+

| min(id) |

+---------+

| 1 |

+---------+

1 row in set (0.00 sec)

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

例如 :

查询女生的编号之和

select sum(id) from students where gender=1;

+---------+

| sum(id) |

+---------+

| 15 |

+---------+

1 row in set (0.00 sec)

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

例如 :

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

select avg(id) from students where gender=1 and isDelete=0;

+---------+

| avg(id) |

+---------+

| 3.0000 |

+---------+

1 row in set (0.00 sec)

聚合与子查询 :

例如 :

获取id醉的的女生的所有信息

select * from students where id=(select max(id) from students where gender=1);

+----+------+--------+---------------------+----------+----------+

| id | name | gender | brithday | isDelete | hometwon |

+----+------+--------+---------------------+----------+----------+

| 5 | 雕 | | 1994-02-14 00:00:00 | | NULL |

+----+------+--------+---------------------+----------+----------+

1 row in set (0.01 sec)

把聚合查询的结果作为外面的查询语句的一个条件。

五、分组 【给查询 的结果做分组,非聚合字段不能出现在结果集中】

按照字段分组,表示此字段相同的数据会被放到一个组中

分组后 ,只能查询出相同的数据列,对于有差异的数据列无法出现在结果集中

可以对分组后的数据进行统计,做聚合运算

语法 :

select 列1,列2,聚合... from 表名 group by 列1,列2,列3....

其中 group by 后面就是分组的依据,比如某个班男的站一队,女的站一队,

那么分组的依据就是男女(性别),分组的目的是为了聚合(更好的统计数据 )。

例如 :

查询男生和女生各自的总数

select gender as '性别' ,count(*) from students group by gender;

+--------+----------+

| 性别 | count(*) |

+--------+----------+

| | 1 |

| | 4 |

+--------+----------+

2 rows in set (0.00 sec)

六、分组后的数据筛选

语法:

select 列1,列2,聚合... from 表名 group by 列1,列2,列3.... having 列1,...,聚合,...

having 后面的条件运算符与 where 的相同。

例如 :

查询男生总人数

select gender as '性别' ,count(*) from students group by gender having gender=1;

+--------+----------+

| 性别 | count(*) |

+--------+----------+

| | 4 |

+--------+----------+

1 row in set (0.00 sec)

这个 having 是对 分组后形成的(结果集)进行筛选的。

而 where 是对 from 面的数据(原始集)进行筛选的。

select gender as '性别' ,count(*) from students where gender=1;

七、排序

为了方便查看 数据,可以对数据进行排序

语法 :

select * from 表名 order by 列1 asc|desc,列2 asc|desc,...

将行数据按照列1进行排序,如果某些行的列1的值相同,则按照列2进行排序,以此类推。

默认 按照列值从小到大排序

asc 从小到大 排序,即 升序

desc 从大到小排序,即 降序

例如 :

查询未删除的男生信息,按 学号降序

select * from students where isDelete=0 and gender=1 order by id desc;

八、分页

1.获取部分行

当数据量过大时,在一页中查看数据是一件非常麻烦的事情,

语法 :

select * from 表名 limit start,count;

从 start 开始,获取 count 条数据。

start索引从 0 开始(这个编号跟 id 无关)

示例 : 分页

已知,每页显示 m 条数据,当前显示第 n 页

求总页数:

查询 总条数 p1

使用 p1 除以 m 得到 p2

如果整除则 p2 为页数

如果 不整除则 p2+1 为总页数

求第 n 页的数据

select * from students where isDelete=0 limit (n-1)*m,m;

猜你喜欢

转载自blog.csdn.net/qq_26128879/article/details/82595015