0
点赞
收藏
分享

微信扫一扫

数据库——MySQL高级SQL语句(一)

@toc

学习准备

先创建两张表,用于语句实验

mysql -uroot -p
show databases;

create database plane;
use plane;
create table destination (
region char(20),
place_name char(20));

insert into destination values('southwest','chengdu');
insert into destination values('north China','beijing');
insert into destination values('southwest','kunming');
insert into destination values('north China','tianjin');
select * from destination;

create table info (
place_name char(20),
sales int(10),
date char(10));

insert into info values('chengdu','350','2021-02-10');
insert into info values('beijing','294','2021-02-10');
insert into info values('kunming','330','2021-02-10');
insert into info values('beijing','392','2021-02-16');
select * from info;

image.png
image.png

一、MySQL高级语句

1、SELECT------显示表格中一个或数个栏位的所有资料

语法:SELECT "栏位" FROM "表名";
例:
select place_name from destination;

image.png

2、DISTINCT------不显示重复的资料

语法:SELECT DISTINCT "栏位" FROM "表名";
例:
select distinct place_name from info;

image.png

3、WHERE------有条件查询

语法:SELECT "栏位" FROM "表名" WHERE "条件";
例:
select place_name from info where sales > 300;

image.png

4、AND|OR------且|或

语法:SELECT "栏位" FROM "表名" WHERE "条件1" {[AND|OR] "条件2"}+ ;
例:
select place_name from info where sales > 350 or sales < 300;
select place_name from info where sales > 300 and sales < 350;

image.png

5、IN------显示已知的值的资料

语法:SELECT "栏位" FROM "表名" WHERE "栏位" IN ('值1', '值2', ...);
例:
select * from info where place_name in ('beijing');
select * from info where place_name in ('beijing','kunming');

image.png

6、BETWEEN------显示两个值范围内的资料

语法:SELECT "栏位" FROM "表名" WHERE "栏位" BETWEEN '值1' AND '值2';
例:
select * from info where sales between '300' and '350';

image.png

7、通配符

  • 通常通配符都是跟 LIKE 一起使用的
    % :百分号表示零个、一个或多个字符
    _ :下划线表示单个字符
    例:
    'A_Z':所有以 'A' 起头,另一个任何值的字符,且以 'Z' 为结尾的字符串。例如,'ABZ' 和 'A2Z' 都符合这一个模式,而 'AKKZ' 并不符合 (因为在 A 和 Z 之间有两个字符,而不是一个字符)。
    'ABC%': 所有以 'ABC' 起头的字符串。例如,'ABCD' 和 'ABCABC' 都符合这个模式。
    '%XYZ': 所有以 'XYZ' 结尾的字符串。例如,'WXYZ' 和 'ZZXYZ' 都符合这个模式。
    '%AN%': 所有含有 'AN'这个模式的字符串。例如,'LOS ANGELES' 和 'SAN FRANCISCO' 都符合这个模式。
    '_AN%':所有第二个字母为 'A' 和第三个字母为 'N' 的字符串。例如,'SAN FRANCISCO' 符合这个模式,而 'LOS ANGELES' 则不符合这个模式。

8、LIKE------匹配一个模式来找出我们要的资料

语法:SELECT "栏位" FROM "表名" WHERE "栏位" LIKE {模式};
例:
select * from info where place_name like '_ei%';

image.png

9、ORDER BY------按关键字排序

语法:SELECT "栏位" FROM "表名" [WHERE "条件"] ORDER BY "栏位" [ASC, DESC];
#ASC 是按照升序进行排序的,是默认的排序方式。
#DESC 是按降序方式进行排序。
例:
select place_name,sales,date from info order by sales;
select place_name,sales,date from info order by sales asc;
select place_name,sales,date from info order by sales desc;

image.png

10、函数

(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…)  返回集合中最大的值
least(x1,x2…)     返回集合中最小的值

SELECT abs(-1), rand(), mod(5,3), power(2,3), round(1.89);
SELECT round(1.8937,3), truncate(1.235,2), ceil(5.2), floor(2.1), least(1.89,3,6.1,2.1);

image.png

(2)聚合函数

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

#count(*)包括了所有的列的行数,在统计结果的时候,不会忽略值为NULL
#count(列名)只包括列名那一列的行数,在统计结果的时候,会忽略列值为NULL的行

例:
select avg(sales) from info;

select count(place_name) from info;
select count(distinct place_name) from info;

select max(sales) from info;
select min(sales) from info;

select sum(sales) from info;

image.png
image.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 反转
  • 补充:如sql_mode开启开启了PIPES_AS_CONCAT,"||"视为字符串的连接操作符而非或运算符,和字符串的拼接函数Concat相类似,这和Oracle数据库使用方法一样的
    image.png
  • trim() 返回去除指定格式的值
    select TRIM ([ [位置] [要移除的字符串] from ] 字符串);
    #[位置]:的值可以为 LEADING (起头), TRAILING (结尾), BOTH (起头及结尾)。 
    #[要移除的字符串]:从字串的起头、结尾,或起头及结尾移除的字符串。缺省时为空格。
    例:
    select trim(leading '123' from '1234567');

    image.png

  • concat(x,y) 将提供的参数 x 和 y 拼接成一个字符串
    
    select concat(region, place_name) from destination;

select region || ' ' || place_name from destination;
select region || ' ' || place_name from destination where place_name = 'beijing';


![image.png](https://s2.51cto.com/images/20220215/1644931280624851.png?x-oss-process=image/watermark,size_14,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=)
- substr(x,y) 获取从字符串 x 中的第 y 个位置开始的字符串,跟substring()函数作用相同
- substr(x,y,z) 获取从字符串 x 中的第 y 个位置开始长度为 z 的字符串

select substr(place_name,3) from destination where place_name = 'beijing';
select substr(place_name,2,4) from destination where place_name = 'beijing';

![image.png](https://s2.51cto.com/images/20220215/1644931557859592.png?x-oss-process=image/watermark,size_14,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=)
- length(x) 返回字符串 x 的长度

select region,length(place_name) from destination;

![image.png](https://s2.51cto.com/images/20220215/1644932578296057.png?x-oss-process=image/watermark,size_14,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=)
- replace(x,y,z) 将字符串 z 替代字符串 x 中的字符串 y

select replace(region,'China',' ') from destination;

![image.png](https://s2.51cto.com/images/20220215/1644932638556096.png?x-oss-process=image/watermark,size_14,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=)
- upper(x) 将字符串 x 的所有字母变成大写字母
- lower(x) 将字符串 x 的所有字母变成小写字母

select upper(region) from destination;
select lower(region) from destination;

![image.png](https://s2.51cto.com/images/20220215/1644932744330665.png?x-oss-process=image/watermark,size_14,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=)
- left(x,y) 返回字符串 x 的前 y 个字符
- right(x,y) 返回字符串 x 的后 y 个字符

select left(region,5) from destination;
select right(region,5) from destination;

![image.png](https://s2.51cto.com/images/20220215/1644932899544497.png?x-oss-process=image/watermark,size_14,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=)
- repeat(x,y) 将字符串 x 重复 y 次

select repeat(region,2) from destination;
select repeat(123,2);

![image.png](https://s2.51cto.com/images/20220215/1644932993200982.png?x-oss-process=image/watermark,size_14,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=)

- space(x) 返回 x 个空格

![image.png](https://s2.51cto.com/images/20220215/1644933024166868.png?x-oss-process=image/watermark,size_14,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=)

- strcmp(x,y) 比较 x 和 y,返回的值可以为-1,0,1
- 小于为-1,等于为0,大于为1
![image.png](https://s2.51cto.com/images/20220215/1644933539898040.png?x-oss-process=image/watermark,size_14,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=)

- reverse(x) 将字符串 x 反转

![image.png](https://s2.51cto.com/images/20220215/1644933573590892.png?x-oss-process=image/watermark,size_14,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=)

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

语法:SELECT "栏位1", SUM("栏位2") FROM "表名" GROUP BY "栏位1";
例:
select place_name,sum(sales) from info group by place_name order by sum(sales);

![image.png](https://s2.51cto.com/images/20220215/1644933934573675.png?x-oss-process=image/watermark,size_14,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=)

### 12、HAVING------用来过滤由GROUP BY 语句返回的记录表
- 通常与GROUP BY语句联合使用
- HAVING语句的存在弥补了WHERE关键字不能与聚合函数联合使用的不足。如果被SELECT的只有函数栏,那就不需要GROUP BY子句。
![image.png](https://s2.51cto.com/images/20220215/1644934042312701.png?x-oss-process=image/watermark,size_14,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=)
### 13、别名------栏位別名,表格別名

语法:SELECT "表格別名"."栏位1" [AS] "栏位別名" FROM "表格名" [AS] "表格別名";
例:
select A.place_name "name",sum(A.sales) "sum_sales" from info A group by name;

![image.png](https://s2.51cto.com/images/20220215/1644934293983417.png?x-oss-process=image/watermark,size_14,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=)
### 14、子查询:连接表格

语法:
#外查询
SELECT "栏位1" FROM "表格1" WHERE "栏位2" [比较运算符]
#内查询
(SELECT "栏位1" FROM "表格2" WHERE "条件");
#可以是符号的运算符,例如:=、>、<、>=、<= ;也可以是文字的运算符,例如 LIKE、IN、BETWEEN

例1:

select sum(sales) from info where place_name in (select place_name from destination where region = 'southwest');
#下面这个句子就是上面的简化版
select sum(sales) from info where place_name in('chengdu','kunming');


![image.png](https://s2.51cto.com/images/20220215/1644934502475563.png?x-oss-process=image/watermark,size_14,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=)

例2:

select sum(a.sales) from info a where a.place_name in(select place_name from destination b where b.place_name = a.place_name);

![image.png](https://s2.51cto.com/images/20220215/1644934598528129.png?x-oss-process=image/watermark,size_14,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=)
![image.png](https://s2.51cto.com/images/20220215/1644934660184203.png?x-oss-process=image/watermark,size_14,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=)
### 15、EXISTS------用来测试内查询有没有产生任何结果
- 类似布尔值是否为真
- 如果有的话,系统就会执行外查询中的SQL语句。若是没有的话,那整个 SQL 语句就不会产生任何结果。

语法:SELECT "栏位1" FROM "表格1" WHERE EXISTS (SELECT FROM "表格2" WHERE "条件");
例:
select sum(sales) from info where exists (select
from destination where region = 'southwest');

![image.png](https://s2.51cto.com/images/20220215/1644934760921593.png?x-oss-process=image/watermark,size_14,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=)

## 二、连接查询
![image.png](https://s2.51cto.com/images/20220215/1644934797543495.png?x-oss-process=image/watermark,size_14,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=)

### 1、inner join(等值相连)
- 只返回两个表中联结字段相等的行

SELECT FROM 表1 表1的别名 INNER JOIN 表2 表2的别名 on 表1别名.栏位 = 表2别名.栏位; #这里的栏位名称相同
例:
select
from destination a inner join info b on a.place_name = b.place_name;

![image.png](https://s2.51cto.com/images/20220215/1644935566881450.png?x-oss-process=image/watermark,size_14,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=)
### 2、left join(左联接)
- 返回包括左表中的所有记录和右表中联结字段相等的记录

SELECT FROM 表1 表1的别名 LEFT JOIN 表2 表2的别名 on 表1别名.栏位 = 表2别名.栏位; #这里的栏位名称相同
例:
select
from destination a left join info b on a.place_name = b.place_name;

![image.png](https://s2.51cto.com/images/20220215/1644935790935430.png?x-oss-process=image/watermark,size_14,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=)

### 3、right join(右联接)
- 返回包括右表中的所有记录和左表中联结字段相等的记录

SELECT FROM 表1 表1的别名 RIGHT JOIN 表2 表2的别名 on 表1别名.栏位 = 表2别名.栏位; #这里的栏位名称相同
例:
select
from destination a right join info b on a.place_name = b.place_name;

![image.png](https://s2.51cto.com/images/20220215/1644935901228695.png?x-oss-process=image/watermark,size_14,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=)
## 三、CREATE VIEW 视图
- 视图跟表格的不同是,表格中有实际储存资料,而视图是建立在表格之上的一个架构,它本身并不实际储存资料。
- 临时表在用户退出或同数据库的连接断开后就自动消失了,而视图不会消失。
- 视图不含有数据,只存储它的定义,它的用途一般可以简化复杂的查询。比如你要对几个表进行连接查询,而且还要进行统计排序等操作,写SQL语句会很麻烦的,用视图将几个表联结起来,然后对这个视图进行查询操作,就和对一个表查询一样,很方便。

语法:CREATE VIEW "视图表名" AS "SELECT 语句";
例:
create view new_info as select a.region region,a.place_name place_name,b.sales sales from destination a inner join info b on a.place_name = b.place_name;

select * from new_info;
drop view new_info;


![image.png](https://s2.51cto.com/images/20220215/1644936101104753.png?x-oss-process=image/watermark,size_14,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=)
举报

相关推荐

0 条评论