@[toc]

一、准备

1.源码编译安装 MySQL

Mysql 服务(一键部署)

2.创建两个表以作例子

mysql -uroot -p123123

create database train_ticket;
#创建库

use train_ticket;
create table REGION(region varchar(10),site varchar(20));
create table FARE(site varchar(20),money int(10),date varchar(15));
#创建表

desc REGION;
desc FARE;
#查看表结构

10.png

insert into REGION values ('south','shenzhen');
insert into REGION values ('south','hongkong');
insert into REGION values ('north','beijing');
insert into REGION values ('north','tianjin');
#在表一中插入数据
select * from REGION;
#查看表中的所有数据

11.png

insert into FARE values ('shenzhen',1000,'2021-01-30');
insert into FARE values ('hongkong',1500,'2021-01-30');
insert into FARE values ('beijing',800,'2021-01-30');
insert into FARE values ('tianjin',500,'2021-01-30');
insert into FARE values ('hongkong',2000,'2021-02-05');
select * from FARE;

12.png

二、SQL 语句高阶运用

1.SELECT

显示表格中一个或数个字段的所有资料
用法:

SELECT 字段 FROM 表名

例:
select region from REGION;
select money from FARE;

10.png

2.DISTINCT

不显示重复的资料,及去重
用法:

ELECT DISTINCT 字段 FROM 表名

例:
select distinct region from REGION;
select distinct date from FARE;

10.png

3.WHERE

有条件查询
用法:

SELECT 字段 FROM 表名 WHERE 条件

例:
select site from FARE where money > 1000;
select site from FARE where money = 1000;
select site from FARE where money < 1000;

11.png

4.AND、OR

且和或
用法:

SELECT 字段 FROM 表名 WHERE 条件1 [AND|OR] (条件2);

例:
select site from FARE where money > 800 and (money < 2000);
select site,money from FARE where money < 600 or (money < 2100 and money >700);
select site,money,date from FARE where money >= 500 and (date < '2021-02-07' and money < 1000);

11.png

5.IN

显示已知的资料
用法:

SELECT 字段 FROM 表名 WHERE 字段 IN ('值1','值2',...);

例:
select site,money from FARE where money in (1500,500);
select money,date from FARE where money in (800,2000);

10.png

6.BETWEEN

显示两个值范围内的资料
用法:

SELECT 字段 FROM 表名 WHERE 字段 BETWEEN '值1' and '值2';

例:
select * from FARE where money between 500 and 2000;
select * from FARE where money between 666 and 1888;

11.png

7.通配符(LIKE)

通常通配符都是跟LIKE一起使用
%:百分号表示零个、一个或多个字符
_:下划线表示单个字符
LIKE:用于匹配模式来查找资料
用法:

SELECT 字段 FROM 表名 WHERE 字段 LIKE ‘模式’;

例:
select * from FARE where site LIKE 'be%';
select * from FARE where site LIKE 'ho%';

11.png

select site,money from FARE where site LIKE '%jin_';
select site,money from FARE where site LIKE '%kon_';

10.png

8.ORDER BY

按关键字排序
用法:

SELECT 字段 FROM 表名 [WHERE 条件] ORDER BY 字段 [ASC,DESC];
#ASC:按照升序进行排序,默认的排序方式
#DESC:按照降序进行排序

例:
select * from FARE order by money desc;
select * from FARE order by money asc;

10.png

select date,money from FARE order by money desc;
select date,money from FARE where money > 999 order by money asc;

三、函数

1.数学函数

具体用法如下表所示:

函数 解释
abs(x) 返回 x 的绝对值
rand() 返回 0 到 1 的随机数
mod(x,y) 返回 x 除以 y 以后的余数
power(x,y) 返回 x 的 y 次方
round(x) 返回离 x 最近的整数
round(x,y) 保留 x 的 y 位小数四舍五入后的值
sqrt(x) 返回 x 的平方根
truncate(x,y) 返回数字 x 截断为 y 位小数的值
ceil(x) 返回大于或等于 x 的最小整数
floor(x) 返回小于或等于 x 的最大整数
greatest(x1,x2…) 返回集合中最大的值
east(x1,x2…) 返回集合中最小的值

例:

select abs(-1),rand(),mod(5,3),power(2,3),round(1.567);
select sqrt(9),truncate(1.2345,2),ceil(1.3);
floor(1.7),greatest(1,2,3,4,5),least(1,2,3,4,5);

12.png

2.聚合函数

具体用法如下表所示:

函数 解释
avg() 返回指定列的平均值
count() 返回指定列中非 NULL 值的个数
min() 返回指定列的最小值
max() 返回指定列的最大值
sum(x) 返回指定列的所有值之和

例:

select avg(money) from FARE;
select count(money) from FARE;
select min(money) from FARE;
select max(money) from FARE;
select sum(money) from FARE;

10.png

3.字符串函数

函数 解释
trim() 返回去除指定格式的值
concat(x,y) 将提供的参数 x 和 y 拼接成一个字符串
substr(x,y) 获取从字符串 x 中的第 y 个位置开始的字符串,跟substring()函数作用相同
substr(x,y,z) 获取从字符串 x 中的第 y 个位置开始长度为 z 的字符串
length(x) 返回字符串 x 的长度
replace(x,y,z) 将字符串 z 替代字符串 x 中的字符串 y
upper(x) 将字符串 x 的所有字母变成大写字母
lower(x) 将字符串 x 的所有字母变成小写字母
left(x,y) 返回字符串 x 的前 y 个字符
right(x,y) 返回字符串 x 的后 y 个字符
repeat(x,y) 将字符串 x 重复 y 次
space(x) 返回 x 个空格
strcmp(x,y) 比较 x 和 y,返回的值可以为-1,0,1
reverse(x) 将字符串 x 反转
  • [位置]:的值可以为 LEADING (起头), TRAILING (结尾), BOTH (起头及结尾)
  • [要移除的字符串]:从字串的起头、结尾,或起头及结尾移除的字符串;缺省时为空格
  • 例:
    select trim(leading 'be' from 'beijing');
    select trim(trailing '--' from 'beijing--');
    select trim(both '--' from '--shenzhen--');

    10.png

select concat(region,site) from REGION where region = 'south';
select concat(region,' ',site) from REGION where region = 'south';

10.png

select substr(money,1,2) from FARE;
select substr(money,1,3) from FARE;

11.png

select length(site) from FARE;
select length(date) from FARE;

11.png

select replace(site,'zhen','--') from FARE;
select replace(site,'on','--') from FARE;

11.png

select upper(site) from FARE;
select upper(region) from REGION;

12.png

select lower('ABCDERG');

15.png

select left(site,2) from FARE;
select right(site,3) from FARE;

12.png

select repeat(site,2) from FARE;
select repeat(region,3) from REGION;

11.png

select space(2);

12.png

select strcmp(100,200);
select strcmp(200,100);
select strcmp(200,200);

12.png

select reverse(site) from FARE;
select reverse(date) from FARE;

12.png

4.'||'连接符

如果在 mysql 的配置文件中,sql_mode 开启开启了 PIPES_AS_CONCAT,则"||"视为字符串的连接操作符而非或运算符
和字符串的拼接函数 Concat 相类似,这和 Oracle 数据库使用方法一样的

10.png

mysql -uroot -p123123
use train_ticket;

select region || ' ' || site from REGION where region = 'north';
select site || ' ' || money || ' ' || date from FARE;

11.png

5.GROUP BY

BY 后面的栏位的查询结果进行汇总分组,通常是结合聚合函数一起使用的
GROUP BY 有一个原则,就是 SELECT 后面的所有列中,没有使用聚合函数的列,必须出现在 GROUP BY 后面
用法:

select site,sum(money) from FARE group by site;
select site,sum(money),date from FARE group by site order by money desc;
select site,count(money),sum(money),date from FARE group by site order by money desc;

12.png

6.HAVING

用来过滤由 GROUP BY 语句返回的记录集,通常与 GROUP BY 语句联合使用
HAVING 语句的存在弥补了 WHERE 关键字不能与聚合函数联合使用的不足
如果被 SELECT 的只有函数栏,那就不需要 GROUP BY 子句
用法:

SELECT 字段1,SUM(字段2) FROM 表名 GROUP BY 字段1 HAVING(函数条件);

例:
select site,count(money),sum(money),date from FARE group by site having sum(money) >=800;

10.png

7.别名

用于字段别名与表格别名
用法:

SELECT “表格別名”.“字段1” [AS] “字段1別名” FROM “表格名” [AS] “表格別名”;

例:
select RE.region  AS reg, count(site) from REGION AS RE group by reg;

select FA.site AS si,sum(money),count(money),date AS da from FARE AS FA group by si;

10.png

8.子查询

连接表格,在 WHERE 子句或 HAVING 子句中插入另一个 SQL 语句

#可以是符号的运算符
#例:=、>、<、>=、<=

#也可以是文字的运算符
#例:LIKE、IN、BETWEEN

例:
select A.site,region from REGION AS A where A.site in(select B.site from FARE AS B where money<2000);

select B.site,money,date from FARE AS B where site in(select A.site from REGION AS A where region = 'north');

15.png

9.EXISTS

用来测试内查询有没有产生任何结果,类似布尔值是否为真
如果有的话,系统就会执行外查询中的SQL语句
若是没有,那整个SQL语句就不会产生任何结果
用法:

SELECT 字段1 FROM 表1 WHERE EXISTS (SELECT * FROM 表2 WHERE 条件);

例:
use train_ticket;

select region from REGION where exists (select * from FARE where money = '800');

select site from REGION where exists (select * from FARE where money = '801');
select * from FARE where money = '800';
select * from FARE where money = '801';
select site from REGION;

12.png

10.连接查询

10.1 inner join——等值相连

只返回两个表中联接字段相等的行
用法:

SELECT 字段 FROM 表1 INNER JOIN 表2 ON 表1.字段 = 表2.字段;

例:
select * from REGION AS A inner join FARE AS B on A.site = B.site;

10.png

10.2 left join——左联接

返回包括左表中所有记录和右表中联接字段相等的记录
例:

SELECT 字段 FROM 表1 LEFT JOIN 表2 ON 表1.字段 = 表2.字段;

例:
select * from REGION AS A left join FARE AS B on A.site = B.site;

11.png

10.3 right join——右联接

返回包括右表中的所有记录和左表中联接字段相等的记录
例:

SELECT 字段 FROM 表1 RIGHT JOIN 表2 ON 表1.字段 = 表2.字段;

例:
update FARE set site='nanjing' where money='2000';
select * from REGION AS A right join FARE AS B on A.site = B.site;

11.png