MySQL数据库高级SQL语句(三)

MySQL数据库高级SQL语句

  • SELECT TRIM
  • 连接查询
  • CREATE VIEW
  • UNION
  • 交集值
  • 无交集值
  • CASE

MySQL数据库高级SQL语句(三)_第1张图片

SELECT TRIM

SELECT TRIM (位置 ‘想移除掉的字符’ from ‘原字符’);
#[位置]: 的值可以为 LEADING(起头),TRAILING(结尾),BOTH(起头及结尾)。
#[想移除掉的字符]: 从字串的起头、结尾,或起头及结尾移除的字符串。缺省时为空格。

select trim(leading '.' from '..abc..');
select trim(trailing '.' from '..abc..');
select trim(both '.' from '..abc..');

MySQL数据库高级SQL语句(三)_第2张图片

连接查询

修改下表内数据,方便观察
update tickets set Store_Name='hunan' where Sales=500;
insert into city (Region,Name) values('south','fujian');
insert into tickets(Store_Name,Sales,Date) values('chengdu','2000','2021-2-01');

MySQL数据库高级SQL语句(三)_第3张图片

inner join (等值相连):只返回两个表中联结字段相等的行
语法:SELECT 字段 FROM 表1 INNER JOIN 表2 ON 表1.字段 = 表2.字段;

left join (左联接):返回包括左表中的所有记录和右表中联结字段相等的记录
语法:SELECT 字段 FROM 表1 LEFT JOIN 表2 ON 表1.字段 = 表2.字段;

right join (右联接):返回包括右表中的所有记录和左表中联结字段相等的记录
语法:SELECT 字段 FROM 表1 RIGHT JOIN 表2 ON 表1.字段 = 表2.字段;

select * from city A inner join tickets B on A.Name = B.Store_Name;
select * from city A left join tickets B on A.Name = B.Store_Name;
select * from city A right join tickets B on A.Name = B.Store_Name;

MySQL数据库高级SQL语句(三)_第4张图片

CREATE VIEW

视图,可以被当作是虚拟表或存储查询。
视图跟表格的不同是,表格中有实际储存资料,而视图是建立在表格之上的一个架构,它本身并不实际储存资料。

临时表在用户退出或同数据库的连接断开后就自动消失了,而视图不会消失。

视图不含有数据,只存储它的定义,它的用途一般可以简化复杂的查询。比如你要对几个表进行连接查询,而且还要进行统计排序等操作,写SQL语句会很麻烦的,用视图将几个表联结起来,然后对这个视图进行查询操作,就和对一个表查询一样,很方便。

语法:CREATE VIEW 视图表名 AS SELECT语句;

select A.Region,sum(B.Sales) from city as A inner join tickets as B on A.Name = B.Store_Name group by Region;
create view C as select A.Region,sum(B.Sales) from city as A inner join tickets as B on A.Name =  B.Store_Name group by Region;

MySQL数据库高级SQL语句(三)_第5张图片

UNION

联集,将两个SQL语句的结果合并起来,两个SQL语句所产生的栏位需要是同样的资料种类

UNION:生成结果的资料值将没有重复,且按照字段的顺序进行排序
语法:[SELECT 语句 1] UNION [ SELECT 语句 2];

select Region from city union select Store_Name from tickets;

MySQL数据库高级SQL语句(三)_第6张图片

UNION ALL:将生成结果的资料值都列出来,无论有无重复
语法:[SELECT 语句 1] UNION ALL[SELECT语句 2];

select Region from city union all select Store_Name from tickets;

MySQL数据库高级SQL语句(三)_第7张图片

交集值

修改表结构及数据记录,方便观察

alter table city change Name Store_Name char(20);
delete from city where Store_Name='fujian';
insert into tickets(Store_Name,Sales,Date) values('chengdu',2000,'2021-2-01');

MySQL数据库高级SQL语句(三)_第8张图片

取两个SQL语句结果的交集

select A.Store_Name from city as A inner join tickets as B on A.Store_Name = B.Store_Name;
select A.Store_Name from city as A inner join tickets as B using(Store_Name);

MySQL数据库高级SQL语句(三)_第9张图片

select Store_Name from city union all select Store_Name from tickets;
 select Store_Name from(select Store_Name from city union all select Store_Name from tickets) A group by A.Store_Name having count(*) > 1;

MySQL数据库高级SQL语句(三)_第10张图片
两表没有重复的行,并且确实有交集的时候

select Name from (select Name from city union all select Store_Name from tickets) A group by A.Name having count(*) > 1;

MySQL数据库高级SQL语句(三)_第11张图片
补充说明:
MySQL数据库高级SQL语句(三)_第12张图片
取两个SQL语句结果的交集,且没有重复

select Store_Name from(select A.Store_Name from city A inner join tickets B on A.Store_Name =B.Store_Name) C group by C.Store_Name;
select distinct A.Store_Name from city as A inner join tickets as B using(Store_Name); 

MySQL数据库高级SQL语句(三)_第13张图片

select distinct A.Store_Name from city as A inner join tickets as B using(Store_Name);
select distinct Store_Name from city where (Store_Name) in (select Store_Name from tickets);
select distinct A.Store_Name from city A left join tickets B using(Store_Name) where B.Store_Name is not null;

MySQL数据库高级SQL语句(三)_第14张图片

无交集值

显示第一个SQL语句的结果,且与第二个SQL语句没有交集的结果,且没有重复

select distinct Store_Name from tickets where(Store_Name) not in (select Store_Name from city);

MySQL数据库高级SQL语句(三)_第15张图片

select A.*,B.* from tickets A left join city B using(Store_Name);
select Store_Name from tickets A left join city B using(Store_Name) where B.Store_Name is null;
select distinct Store_Name from tickets A left join city B using(Store_Name) where B.Store_Nameore_Name is null;

MySQL数据库高级SQL语句(三)_第16张图片

CASE

是 SQL 用来做为 IF-THEN-ELSE之类逻辑的关键字
语法:
SELECT CASE (“栏位名”)
WHEN “条件1” THEN “结果1”
WHEN “条件2” THEN “结果2”

[ELSE “结果N”]
END
FROM “表名”;

"条件"可以是一个数值或是公式。ELSE子句则并不是必须的。

select case Store_Name
when 'beijing' then Sales + 1000
when 'shanghai' then Sales - 1000
else Sales * 10
end
TEST,Store_Name                                                                        
from tickets;

MySQL数据库高级SQL语句(三)_第17张图片

你可能感兴趣的:(数据库,数据库,字符串,mysql,sql)