课程内容如下图所示:
5. Task02:基础查询与排序 - 图1

2.1 select语句基础

2.1.1 select和from语句

从表格中选取数据的时候需要用到select语句,最简单的语句就是select * from TableName。通过select语句查询并且选出数据的过程称之为匹配查询或查询。

基本的select语句包含了selectfrom两个字句,例如从下表clothes中选出name列的数据

  1. /* 语法:select 列名 from 表名 */
  2. /* 例子:选出下面clothes表中的name列 */
  3. select name from clothes

原表clothes如下:
image.png
查询结果如下:
image.png

2.1.2 配合where语句筛选

当不需要取出全部数据,而是选取出满足某些条件的数据时,使用where语句。

select 语句通过where子句来指定查询数据的条件。在where 子句中可以指定“某一列的值和这个字符串相等”或者“某一列的值大于这个数字”等条件。执行含有这些条件的select语句,就可以查询出只符合该条件的记录了。

例子,查找clothes表中price>100的衣服名称,并显示其价格

  1. --使用where语句添加筛选条件
  2. select name, price from clothes where price>100;

筛选后的结果如下:
image.png

2.2 算数运算符和比较运算符

这部分内容比较简单,主要就是根据运算符本身的含义使用,比如>, < 等
5. Task02:基础查询与排序 - 图5
例子,首先将表中的price字段更改为可为NULL

  1. -- 使用modify更改price字段的类型
  2. alter table clothes modify price int null;
  3. desc clothes;

image.png
接下来,将表中的NiKE的价格更改为NULL

  1. update clothes set price=null where name='NiKe';
  2. select * from clothes;

可以看到,修改为NULL
image.png
下面,我们就通过运算符找出其中的price字段不为NULL的衣服名字

  1. select name,price from clothes where price is not null

image.png

2.3 逻辑运算符

5. Task02:基础查询与排序 - 图9

2.3.1 not运算符

老规矩,先更改一下表格中的数据

  1. update clothes set price=120 where name='NiKe';
  2. select * from clothes;

image.png
挑选出其中不大于100的衣服

  1. -- 使用not来表示不怎么样
  2. select name,price from clothes where not price>100;
  3. --等价于下面语句
  4. select name,price from clothes where not price<=100;

2.3.2 and和or运算符

先更改一下clothes这个表,给他新添加一列sales(销量)

  1. -- 使用alter新添加sales列:alter table 表明 add 列名 类型 其他
  2. -- after name表示在name列后添加
  3. alter table clothes add sales int after name;
  4. select * from clothes;

image.png
然后给sales字段添加值

  1. update clothes set sales=1000 where name='adi';
  2. update clothes set sales=2000 where name='LiNing';
  3. update clothes set sales=3000 where name='NiKe';
  4. select * from clothes;

image.png
然后使用and选择sales>2000并且price>120的物品

  1. select * from clothes where sales>2000 and price>100;

image.png

2.4 对表进行聚合查询

sql中用于汇总的函数叫做聚合函数,下面是最常见的聚合函数
5. Task02:基础查询与排序 - 图14
对上面的例子进行一下使用,先看一下最初的clothes表
image.png
使用上面的函数
🔰 查看该表的行数
image.png
🔰计算sales和price的和
image.png
🔰 avg, max等和sum函数同理

2.5 对表进行分组

首先更改数据

  1. -- 先去掉主键,否则name列不允许重复
  2. alter table clothes drop primary key;
  3. -- name添加adi
  4. insert into clothes(name,sales,price) values('adi', 4000, 90);
  5. insert into clothes(name,sales,price) values('adi', 5000, 100);
  6. select * from clothes;

image.png
按名字进行分组,可以分为adi, LiNing和Nike三组,查找每组的最大销量

  1. select name as 名字,max(sales) as 最大销量
  2. from clothes
  3. group by name;

image.png

2.6 为聚合结果指定条件

将表使用group by分组后,怎样才能只取出其中两组?
ch02.07groupby.png
这里WHERE不可行,因为,where子句只能指定记录(行)的条件,而不能用来指定组的条件(例如,数据行数为 2 行或者平均值为 500等)。可以在group by后使用having子句,其中having的用法类似where

之前,我们查找了每个品牌的最大销量

  1. select name as 名字,max(sales) as 最大销量
  2. from clothes
  3. group by name;

image.png
现在,我们选出销量>2500的品牌,相当于对上面的表进行又一步处理,这个时候就得用到having关键字

  1. select name as 名字,max(sales) as 最大销量
  2. from clothes
  3. group by name
  4. having 最大销量>2500;

image.png
现在,我们拿where取出名字为adi的那一列(因为,where只能拿来确定列的条件,这个列还得是之前和现在都有的列,比如现在的name)

  1. select name as 名字,max(sales) as 最大销量
  2. from clothes
  3. where name='adi'
  4. group by name
  5. having 最大销量>2500;

image.png

2.7 对查询结果进行排序

看一下例子,怎么将clothes表中的数据,名字放在一块,按照sales降序排列
image.png
当name相同的时候,按照sales的规则进行排列

  1. select * from clothes order by name asc, sales desc;

image.png

2.8 习题

关于习题部分,2.3和2.4比较有趣,但是也属于一类提,这里给出一个答案。比如,我的clothes表如下
image.png
找出sales比price大1000的数据

  1. select * from clothes where (sales-price)>1000;

image.png