首页 > 数据库 >MySQL高级SQL语句

MySQL高级SQL语句

时间:2024-06-20 23:01:04浏览次数:20  
标签:语句 info name SQL 字段 select MySQL where store

目录

一.准备两个表

二.高级查询方式

1.select

2.distinct

3.where

4.and or

5.in

6.between

7.通配符

8.like

三.运用函数查询

1.常用数学函数

2.聚合函数

3.字符串函数

四.高级查询函数

1.order by

2.group by

3.having

4.别名设置查询

4.1.字段别名

4.2.表别名

5.子查询语句 

6.exists(类查询)

7.sql语句执行顺序

五.表连接查询

1.inner join(内连接)

2.left join(左连接)

3.right join(右连接)

4.full outer join(全外连接)

5.union(联集)

6.union all(联集)

六.求交集

1.内连接

2.左连接

3.右连接

4.子查询

5.多表查询

6.联集+分组

七.求无交集

1.求左表无交集

2.求右表无交集

3.求两个表无交集


一.准备两个表

use mydb;
create table location (Region char(20),Store_Name char(20));
insert into location values('East','Boston');
insert into location values('East','New York');
insert into location values('West','Los Angeles');
insert into location values('West','Houston');

create table store_info (Store_Name char(20),Sales int(10),Date char(10));
insert into store_info values('Los Angeles','1500','2020-12-05');
insert into store_info values('Houston','250','2020-12-07');
insert into store_info values('Los Angeles','300','2020-12-08');
insert into store_info values('Boston','700','2020-12-08');
insert into store_info values('Washington','1000','2020-12-09');
insert into store_info values('Chicago','800','2020-12-10');

二.高级查询方式

1.select

select:显示表格中一个或数个字段的所有数据记录

语法:select 字段1,字段2 from 表名;

select store_name from store_info;

2.distinct

distinct:不显示重复的数据记录

语法:select distinct "字段" from "表名";

select distinct store_name from store_info;

3.where

where:有条件查询

语法:select "字段" from 表名" where "条件";

select * from store_info where sales > 1000;

4.and or

and or:且 或

语法:select "字段" from "表名" where "条件1"  and "条件2";

      select "字段" from "表名" where "条件1"  or "条件2";

select * from store_info where sales > 1000 or (sales >=200 and sales <=500)

5.in

in:显示已知的值的数据记录

语法:select "字段" from "表名" where "字段" in ('值1', '值2', ...);   #in,遍历一个取值列表

select * from store_info where store_name in ('Houston','Los Angeles');

6.between

between:显示两个值范围内的数据记录

语法:select "字段" from "表名" where "字段" between '值1' and '值2';

SELECT * FROM Store_Info WHERE Date BETWEEN '2020-12-06' AND '2020-12-10';

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' 则不符合这个模式。

如: select * from store_info where store_name like '__us%';
select * from store_info where store_name like '_os%';

8.like

like:匹配一个模式来找出我们要的数据记录

语法:select "字段" from "表名" where "字段" like {模式};

select * from store_info where store_name like '%on%';

三.运用函数查询

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);

2.聚合函数

avg()返回指定列的平均值
count()返回指定列中非 NULL 值的个数
min()返回指定列的最小值
max()返回指定列的最大值
sum(字段)返回指定列的所有值之和
###返回指定列sales的平均值
select avg(sales) from store_info;

###返回指定列store_name列中非NULL值的个数
select count(store_name) from store_info;
select count(distinct store_name) from store_info;

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 反转
select concat(region,store_name) from location where store_name='Los Angeles';

select concat(region,' ',store_name) from location where store_name='Los Angeles';

#如sql_mode开启了PIPES_AS_CONCAT,"||"视为字符串的连接操作符而非或运算符,和字符串的拼接函数Concat相类似,这和Oracle数据库使用方法一样的
select region || ' ' || store_name from location where store_name='Los Angele

select substr(store_name,3) from location where store_name='Los Angeles';
select * from location;
select substr(store_name,3,4) from location where store_name='Los Angeles';

select trim([ [位置] [要移除的字符串] from ] 字符串);
#[位置]:的值可以为leading(起头),trailing(结尾),both(起头及结尾)。 
#[要移除的字符串]:从字串的起头、结尾,或起头及结尾移除的字符串。缺省时为空格。

select trim(leading 'Ne' from 'New York');
select trim(trailing 'aw' from 'Newaw Yorkaw');
select trim(both 'aw' from 'awNewaw Yorkaw');

select region,length(store_name) from location;

select replace(region,'ast','astern') from location;

四.高级查询函数

1.order by

order by:按关键字排序

语法:select "字段" from "表名" [where "条件"] order by "字段" [asc, desc];
#asc是按照升序进行排序的,是默认的排序方式。
#desc是按降序方式进行排序。

select * from store_info order by sales desc;

2.group by

group by:对group by后面字段的查询结果,进行汇总分组,通常是结合聚合函数一起使用的

注意

  • group by有一个原则,凡是在group by后面出现的字段,必须在select后面出现
  • 凡是在 SELECT 后面出现的、且未在聚合函数中出现的字段,必须出现在 GROUP BY 后面
语法:select "字段1", sum"字段2") from "表名" group by "字段1";

select store_name,sum(sales) from store_info group by store_name order by sales desc;

3.having

having:用来过滤由group by语句返回的记录集,通常与group by语句联合使用

注意

  • HAVING 语句的存在弥补了 WHERE 关键字不能与聚合函数联合使用的不足
语法:select "字段1", sum("字段2") from "表格名" group by "字段1" having (函数条件);

select store_name,sum(sales) from store_info group by store_name having sum(sales) > 1000;

4.别名设置查询

别名:字段別名 表格別名

语法:select "表格別名"."字段1" [as] "字段別名" from "表格名" [as] "表格別名";

select A.store_name store,sum(A.sales) "total sales" from store_info A group p by A.store_name;

4.1.字段别名

4.2.表别名

5.子查询语句 

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

语法:select "字段1" from "表格1" where "字段2" [比较运算符] 			#外查询
(select "字段1" from "表格2" where "条件");							#内查询

#可以是符号的运算符,例如 =、>、<、>=、<= ;也可以是文字的运算符,例如 LIKE、IN、BETWEEN
select sum(sales) from store_info where store_name in (select store_name from location where region='West');

select sum(A.sales) from store_info A where A.store_name in (select store_name from location B where B.store_Name = A.store_name);

6.exists(类查询)

exists:用来根据条件过滤查询结果,并只返回满足条件的行

注意

  • 这里的子查询作为条件进行判断。如果子查询返回至少一行结果,则外部查询的结果将包含该行
语法:select "字段1" from "表格1" where exists (select * from "表格2" where "条件");

select sum(sales) from store_info where exists (select * from location where region = 'West');

7.sql语句执行顺序

五.表连接查询

  • MYSQL数据库中常用的表连接有三种

1.inner join(内连接)

  • 只返回两个表中联结字段相等的行(有交集的值)

2.left join(左连接)

  • 返回包括左表中的所有记录和右表中联结字段相等的记录
  • 不相等的行返回NULL

3.right join(右连接)

  • 返回包括右表中的所有记录和左表中联结字段相等的记录
  • 不相等的行返回NULL

4.full outer join(全外连接)

  • 返回左表和右表中所有的行记录,MySQL不支持

5.union(联集)

  • 将两个select查询语句的结果合并,并去重

6.union all(联集)

  • 将两个select查询语句的结果合并,不去重
     

六.求交集

1.内连接

select A.字段 from 左表 A inner join 右表 B on A.字段=B.字段;

select A.字段 from 左表 A inner join 右表 B using(字段);

2.左连接

select B.字段 from 左表 A left join 右表 B on A.字段 = B.字段 where B.字段 is not null;

3.右连接

select A.字段 from 左表 A left join 右表 B on A.字段 = B.字段 where A.字段 is not null;

4.子查询

select A.字段 from 左表 A where A.字段 in (select B.字段 from 右表 B);

select A.字段 from 左表 A where exists (select B.字段 from 右表 B where A.字段 = B.字段);

5.多表查询

select A.字段 from 左表 A, 右表 B where A.字段 = B.字段;

6.联集+分组

select A.字段 from (select distinct 字段 from 左表 union all select distinct 字段 from 右表) A group by A.字段 having count(A.字段) > 1;
#此处的 A表 是由内查询语句返回的结果 抽象出的派生表

七.求无交集

1.求左表无交集

select A.字段 from 左表 A left join 右表 B on A.字段 = B.字段 where B.字段 is null;

select 字段 from 左表 where 字段 not in (select 字段 from 右表);

select A.字段 from 左表 A where not exists (select B.字段 from 右表 B where A.字段 = B.字段);

2.求右表无交集

select B.字段 from 左表 A right join 右表 B on A.字段 = B.字段 where A.字段 is null;

select 字段 from 右表 where 字段 not in (select 字段 from 左表);

select A.字段 from 右表 A where not exists (select B.字段 from 左表 B where A.字段 = B.字段);

3.求两个表无交集

select A.字段 from 左表 A left join 右表 B on A.字段 = B.字段 where B.字段 is null
union all
select B.字段 from 左表 A right join 右表 B on A.字段 = B.字段 where A.字段 is null;

select A.字段 from (select distinct 字段 from 左表 union all select distinct 字段 from 右表) A group by A.字段 having count(A.字段) = 1;

标签:语句,info,name,SQL,字段,select,MySQL,where,store
From: https://blog.csdn.net/F12138X/article/details/139821236

相关文章

  • MySQL中设置两个默认时间(createTime字段和updateTime字段)
    MySQL中设置两个默认时间在MySQL中,您可以使用DATETIME数据类型并设置默认值为CURRENT_TIMESTAMP来实现这一点。以下是一个示例,展示了如何为createTime和updateTime字段设置默认值:CREATETABLEtable_name(idINTPRIMARYKEY,createTimeDATETIMEDEFAULTCURRENT......
  • MySQl配置环境变量
    配置环境变量(a)添加一个系统变量,变量名:CATALINA-HOME,变量值:MySql在自己电脑当中的安装路径,注意:5.7版本需要配置的路径是MySQL.Sever5.7的文件夹路径。 (b)在Path变量的结尾添加一个英文分号,之后把上面添加的路径导入进去(%CATALINA-HOME%)在这个结尾处添加\bin.......
  • JDBC(简介、入门与IDEA中导入MySQL的驱动)
    (建议学完MySQL的基础部分)JDBC——简而言之:用Java语言操作数据库。JavaDataBaseConnectivity(Java语言连接数据库)目录一、引言(1)基本介绍(2)JDBC简介1、JDBC概念2、详细介绍3、JDBC的本质二、JDBC快速入门(1)用Java代码操作对应的MySQL数据库的基本流程(2......
  • MySQL高级SQL语句
    目录1.MySQL进阶查询1.1select1.2distinct1.3where1.4andor1.5in1.6between1.7通配符1.8like1.9ORDERBY2.MySQL数据库函数2.1数学函数2.2聚合函数2.3字符串函数2.3.1upper、lower大小写转换2.3.2concat拼接2.3.3substr字符串截取2.3.3len......
  • Sqlite3数据库基本使用
    文章目录一、基本概念二、安装三、使用(一)系统命令(二)关系型数据库表(三)sql语句1.建表语句2.插入语句3.查询语句4.更新语句5.删除语句6.删除表7.对列的操作(一般不会用到)8.主键9.其他命令四、常用的API接口函数(一)API接口函数1.打开和关闭数据库文件2.获取错误信......
  • 学习MySQL数据库:理解与实践
    学习MySQL数据库:理解与实践MySQL是一款开源的关系型数据库管理系统,广泛应用于各类应用程序中,从个人项目到大型企业解决方案。在本文中,我将分享我在学习和使用MySQL过程中的一些心得体会,希望能为初学者提供一些有价值的信息和技巧。1.MySQL的选择与简介MySQL作为开源数据......
  • MySQL数据库基础
    1.数据库概论无论是传统的软件,还是互联网网站,或者是移动端的应用,都要处理数据。数据库技术研究如何有效地管理和存取大量的数据资源。随着计算机技术的不断发展,数据库技术已成为计算机科学的重要分支。今天,数据库技术不仅应用于事务处理,还进一步应用于情报检索、人工智能、专......
  • 在SQL中使用explode函数展开数组的详细指南
    目录简介示例1:简单数组展开示例2:展开嵌套数组示例3:与其他函数结合使用处理结构体数组示例:展开包含结构体的数组示例2:展开嵌套结构体数组总结简介在处理SQL中的数组数据时,explode函数非常有用。它可以将数组中的每个元素单独提取出来,便于进一步处理。本文将通过几......
  • 全是坑!!从 MySQL 到 PostgreSQL【送源码】
    0、前言原项目框架SpringBoot+ MybatisPlus +Mysql1、切换流程1.1、项目引入postgresql驱动包由于我们要连接新的数据库,理所当然的要引入该数据库的驱动包,这与mysql驱动包类似<dependency>    <groupId>org.postgresql</groupId>    <artifactId>postgresq......
  • MySQL进阶知识之存储过程、函数、流程控制、索引
    【一】MySQL进阶知识之存储过程【1】什么是存储过程存储过程就类似于Python中的自定义函数内部包含了一系列可以执行的SQL语句,存储过程存储在MySQL服务端中,可以通过调用存储过程触发内部的SQL语句存储过程是在关系型数据库中存储的一组预定义的SQL语句集合,可以接收参数并返回......