MySQL高级SQL语句
- 1.MySQL进阶查询
- 1.1 select
- 1.2 distinct
- 1.3 where
- 1.4 and or
- 1.5 in
- 1.6 between
- 1.7 通配符
- 1.8 like
- 2.MySQL数据库函数
- 2.1 数学函数
- 2.2 聚合函数
- 2.3 字符串函数
- 3.查询函数
- 3.1 order by
- 3.2 group by
- 3.3 sql语句执行顺序
- 3.4 having
- 3.5 别名(字段别名 表别名)
- 3.6 子查询
- 3.7 exists(类查询)
1.MySQL进阶查询
数据库实例准备:
create database kgc;
use kgc;
###在数据库kgc中创建数据表location,并添加数据记录
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');
###在数据库kgc中创建数据表store_info,并添加数据记录
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');
1.1 select
select:显示表格中一个或数个字段的所有数据记录
语法:SELECT "字段" FROM "表名";
select store_name from store_info;
1.2 distinct
distinct:不显示重复的数据记录
语法:SELECT DISTINCT "字段" FROM "表名";
select distinct store_name from store_info;
1.3 where
where:有条件查询
语法:SELECT "字段" FROM "表名" WHERE "条件";
select * from store_info where sales > 1000;
1.4 and or
and or:且 或
语法:SELECT "字段" FROM "表名" WHERE "条件1" {[AND|OR] "条件2"}+ ;
select * from store_info where sales > 1000 or (sales >=200 and sales <=500);
1.5 in
in:显示已知的值的数据记录
语法:SELECT "字段" FROM "表名" WHERE "字段" IN ('值1', '值2', ...);
select * from store_info where store_name in ('Houston','Los Angeles');
1.6 between
between:显示两个值范围内的数据记录
语法:SELECT "字段" FROM "表名" WHERE "字段" BETWEEN '值1' AND '值2';
SELECT * FROM Store_Info WHERE Date BETWEEN '2020-12-06' AND '2020-12-10';
1.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%';
1.8 like
like:匹配一个模式来找出我们要的数据记录
语法:SELECT "字段" FROM "表名" WHERE "字段" LIKE {模式};
select * from store_info where store_name like '%on%';
2.MySQL数据库函数
2.1 数学函数
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);
获取1000以内的随机整数;
select rand() * 1000;
###以下获取1000以内随机整数的两种方法
select truncate(rand() * 1000,0);
select round(rand() * 1000);
2.2 聚合函数
###返回指定列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;
2.3 字符串函数
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;
3.查询函数
3.1 order by
order by:按关键字排序
语法:SELECT "字段" FROM "表名" [WHERE "条件"] ORDER BY "字段" [ASC, DESC];
#ASC是按照升序进行排序的,是默认的排序方式。
#DESC是按降序方式进行排序。
select * from store_info order by sales desc;
3.2 group by
group by:对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;
**注意:** **group by有一个原则,凡是在group by后面出现的字段,必须在select后面出现;**
凡是在select后面出现的、且未在聚合函数中出现的字段,必须出现在group by后面出现。
3.3 sql语句执行顺序
sql语句执行顺序from >> on >> join >> where >> group by >> having >> distinct >> order by >> limit.
3.4 having
**having:用来过滤由group by语句返回的记录集,通常与group by语句联合使用
**
语法: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;
注意:
having语句的存在弥补了where关键字不能与聚合函数联合使用的不足。
3.5 别名(字段别名 表别名)
别名:字段別名 表格別名
语法: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;
3.6 子查询
子查询:连接表格,在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);
3.7 exists(类查询)
exists:用来测试内查询有没有产生任何结果,类似布尔值是否为真
#如果有的话,系统就会执行外查询中的SQL语句。若是没有的话,那整个 SQL 语句就不会产生任何结果。
语法:SELECT "字段1" FROM "表格1" WHERE EXISTS (SELECT * FROM "表格2" WHERE "条件");
select sum(sales) from store_info where exists (select * from location where region = 'West');