MySQL一些常用高级SQL语句

吾爱主题 阅读:159 2024-04-02 08:03:59 评论:0

mysql高级sql语句

?
1 2 3 4 5 6 use kgc; 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' ) ;

?
1 2 3 4 5 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' );

select

?
1 2 3 ---- select ---- 显示表格中一个或数个栏位的所有资料 语法: select "栏位" from "表名" ; select store_name from store_info;

distinct

?
1 2 3 ---- distinct ---- 不显示重复的资料 语法: select distinct "栏位" from "表名" ; select distinct store_name from store_info;

where

?
1 2 3 ---- where ---- 有条件查询 语法: select "栏位” from " 表名 " where " 条件"; select store_name from store_info where sales > 1000;

and or

?
1 2 3 ---- and or ---- 且 或 语法: select "栏位" from "表名” where " 条件1 " {[andior] " 条件2"}+; select store_name from store_info where sales > 1000 or (sales < 500 and sales > 200);

in

?
1 2 3 ---- in ---- 显示己知的值的资料 语法: select "栏位" from "表名" where "栏位" in ( '值1' , '值2' , ...); select * from store_info where store_name in ( 'los angeles' , 'houston' );

between

?
1 2 3 ---- between ---- 显示两个值范围内的资料 语法: select "栏位" from "表名" where "栏位" between '值1' and '值2' ; select * from store_info where date between '2020-12-06' and '2020-12-10' ;

通配符

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 ---- 通配符 ---- 通常通配符都是跟like一起使用的 % : 百分号表示零个、一个或多个字符 _ : 下划线表示单个字符   'a_z' :所有以‘a '起头,另一个任何值的字符,且以z' 为结尾的字符串。例如, 'a.bz' 和‘a.22 '都符合这一个模式,而‘akk2' 并不符合(因为在a和z之间有两个字符,而不是一个字符)。 'abc%' :所有以 'abc' 起头的字符串。例如, 'abcd' 'abcabc' 都符合这个模式。 '%xyz' :所有以 'xyz' 结尾的字符串。例如, 'wxyz' 和‘zzxyz '都符合这个模式。 ' %an% ':所有含有' an '这个模式的字符串。例如,' los angeles '和' san francisco '都符合这个模式。 ' _an% ':所有第二个字母为‘a' 和第三个字母为 'n' 的字符串。例如, 'samn francitsco' 符合这个模式,而 'los angeles' 则不符合这个模式。 ---- like ---- 匹配一个模式来找出我们要的资料 语法: select "栏位" from "表名" where "栏位" like {模式}; select * from store_info where store_name like '%os%' ; ---- order by ---- 按关键字排序 语法: select "栏位" from "表名" [ where "条件" ] order by "栏位" [ asc desc ]; # asc 是按照升序进行排序的,是默认的排序方式。 # desc 是按降序方式进行排序。 select store_name,sales, date from store_info order by sales desc ;

函数 数学函数

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 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的最小整数 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);

聚合函数

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 聚合函数: avg ()       #返回指定列的平均值 count ()     #返回指定列中非 null 值的个数 min ()       #返回指定列的最小值 max ()       #返回指定列的最大值 sum (x)      #返回指定列的所有值之和   select avg (sales) from store_info;   select count (store_name) from store_info; select count ( distinct store_name) from store_info;   select max (sales) from store_info; select min (sales) from store_info;   select sum (sales) from store_info;   select count ( distinct store_name) from store_info; select count (*) from store_info; # count (*)包括了所有的列的行数,在统计结果的时候,不会忽略列值为 null # count (列名)只包括列名那一列的行数,在统计结果的时候,会忽略列值为 null 的行

字符串函数

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 字符串函数: 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 = 'boston' ;   #如sql_mode开启开启了pipes_as_concat, "||" 视为字符串的连接操作符而非或运算符,和字符串的拼接函数concat相类似,这和oracle数据库使用方法一样的 select region || ' ' || store_name from location where store_name = 'boston' ;   select substr(store_name,3) from location where store_name = 'los angeles' ; select substr(store_name,2,4) from location where store_name = 'new york' ;   select trim ([ [位置] [要移除的字符串] from ] 字符串); #[位置]:的值可以为 leading(起头),trailing(结尾),both(起头及结尾)。 #[要移除的字符串]:从字串的起头、结尾,或起头及结尾移除的字符串。缺省时为空格   select trim(leading 'ne' from 'new york' );   select region,length(store_name) from location;   select replace (region, 'ast' , 'astern' ) from location;


?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 ---- group by ----  对group by后面的栏位的查询结果进行汇总分组,通常是结合聚合函数一起使用的 group by 有一个原则,就是 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 ;   ---- having ---- 用来过滤由group by语句返回的记录集,通常与group by语句联合使用 having 语句的存在弥补了 where 关键字不能与聚合函数联合使用的不足。如果被selecct的只有函数栏,那就不需要 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) > 1500;   ---- 别名 ---- 栏位别名表格别名   语法: select "表格别名" . "栏位1” [as] " 栏位别名 " from " 表格名 " [as] " 表格别名 " select a.store_name store,sum(a.sales) " total sales " from store_info a group by a.store_name;   ---- 子查询 ---- 连接表格,在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);

exists

?
1 2 3 4 ---- 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' );

连接查询

location 表格

?
1 update store_info set store_name= 'washington' where sales=300;

store_info表格

?
1 2 3 4 5 6 7 8 9 10 11 12 inner join (内连接):只返回两个表中联结字段相等的行 left join (左连接):返回包括左表中的所有记录和右表中联结字段相等的记录 right join (右连接):返回包括右表中的所有记录和左表中联结字段相等的记录   select * from location a inner join store_info b on a.store_name = b.store_name ;   select * from location a right join store_info b on a.store_name = b.store_name ;   select * from location a,store_info b where a.store_name = b.store_name;   select a.region region, sum (b.sales) sales from location a,store_info b where a.store_name = b.store_name group by region;

create view

?
1 2 3 4 5 6 7 8 9 10 11 ---- create view ---- 视图,可以被当作是虚拟表或存储查询。 ·视图跟表格的不同是,表格中有实际储存资料,而视图是建立在表格之上的一个架构,它本身并不实际储存资料。 ·临时表在用户退出或同数据库的连接断开后就自动消失了,而视图不会消失。 ·视图不含有数据,只存储它的定义,它的用途一般可以简化复杂的查询。比如你要对几个表进行连接查询,而且还要进行统计排序等操作,写sqt语句会很麻烦的,用视图将几个表联结起来,然后对这个视图进行查询操作,就和对一个表查询一样,很方便。   语法: create view "视图表名” as " select 语句"; create view v_region_sales as select a.region region, sum (b.sales) sales from location a inner join store_info b on a.store_name = b.store_name group by region;   select * from v_region_sales; drop view v_region_sales;

union

?
1 2 3 4 5 6 7 8 9 10 ---- union ---- 联集,将两个sql语句的结果合并起来,两个sqi语句所产生的栏位需要是同样的资料种类 union :生成结果的资料值将没有重复,且按照字段的顺序进行排序 语法:[ select 语句 1] union [ select 语句2];   union all :将生成结果的资料值都列出来,无论有无重复 语法:[ select 语句 1] union all [ select 语句 2];   select store_name from location union select store_name from store_info;   select store_name from location union all select store_name from store_info;

交集值

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 ----- 交集值 ---- 取两个sql语句结果的交集 select a.store_name from location a inner join store_info b on a.store_name = b.store_name; select a.store_name from location a inner join store_info b using(store_name);   #两表基中的一个表没有指定的行,而另一个表这个行有重复,并且确实有交集的时候用 select a.store_name from ( select store_name from location union all select store_name from store_info) a group by a.store_name having count (*) > 1;   #取两个sql语句结果的交集,且没有重复 select a.store_name fronm ( select b.store_name from location b inner join store_info c on b.store_name = c.store_name) a group by a.store_name;   select distinct a.store_name from location a inner join store_info b using(store_name);   select distimct store_name from location where (store_name) in ( select store_name from store_info);   select distinct a.store_name from location a left join store_info b using(store_name) where b.store_name is not null ;

无交集值

?
1 2 3 4 ---- 无交集值 ---- 显示第一个sql语句的结果,且与第二个sql语句没有交集的结果,且没有重复 select distinct store_name from location where (store_name) not in ( select store_name from store_info);   select distinct a.store_name from location a left join store_info b using(store_name) where b.store_name is null ;

case

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 ---- case ---- 是 sql 用来做为 if-then-else 之类逻辑的关键字 语法: select case ( "栏位名" )    when "条件1” then " 结果1 "    when " 条件2 " then " 结果2 "    ...    [else " 结果n "]    end from " 表名 ";   #" 条件 "可以是一个数值或是公式。else子句则并不是必须的。   select store_name, case store_name    when 'los angeles' then sales * 2    when 'boston' then sales * 1.5    else sales    end " new sales ",date from store_info;   #" new sales"是用于 case 那个栏位的栏位名。   create table total_sales ( name char (10),sales int (5)); insert into total_sales values ( 'zhangsan' ,10); insert into total_sales values ( 'lisi' ,15); insert into total_sales values ( 'wangwu' ,20); insert into total_sales values ( 'zhaoliu' ,40); insert into total_sales values ( 'sunqi' ,50); insert into total_sales values ( 'zhouba' ,20); insert into total_sales values ( 'wujiu' ,30);


1、算排名

?
1 2 3 4 5 6 7 8 9 10 11 ----算排名----表格自我连结(self join),然后将结果依序列出,算出每一行之前〈包含那一行本身)有多少行数 select a1. name , a1.sales, count (a2.sales) rank from total_sales a1,total_sales a2 where a1.sales < a2.sales 0r (a1.sales = a2.sales and a1. name = a2. name ) group by a1. name , a1.sales order by a1.sales desc ;   例如: select a1. name ,a1.sales, count (a2.sales) rank from total_sales a1,total_sales a2 where a1.sales <  a2.sales or (a1.sales=a2.sales and a1. name =a2. name ) group by a1. name order by a1.sales desc ;   解释: 当a1的sales字段值小于a2的sales字段值、或者两表sales字段值相等并且 name 字段值相等时, 从a1和a2表中查询a1的 name 字段值、a1的sales字段值、和a2的sales字段的非空值rank是别名,并为a1的 name 字段分组,a1的sales字段降序排序

2、算中位数

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 ---- 算中位数 ---- select sales middle from ( select a1. name ,a1.sales, count (a2.sales) rank from total_sales a1,total_sales a2 where a1.sales < a2.sales 0r (a1.sales = a2.sales and a1. name >= a2. name ) group by a1. name ,a1.sales order by a1.sales desc ) a3 where a3.rank = ( select ( count (*)+1) div 2 from total_sales);   例如: select * from ( select a1. name ,a1.sales, count (a2.sales) rank from total_sales a1,total_sales a2 where a1.sales < a2.sales or (a1.sales=a2.sales and a1. name =a2. name ) group by a1. name order by a1.sales desc ) a3 where a3.rank = ( select ( count (*)+1) div 2 from total_sales);   select sales mid from ( select a1. name ,a1.sales, count (a2.sales) rank from total_sales a1,total_sales a2 where a1.sales < a2.sales or (a1.sales=a2.sales and a1. name =a2. name ) group by a1. name order by a1.sales desc ) a3 where a3.rank = ( select ( count (*)+1) div 2 from total_sales);     #每个派生表必须有自己的别名,所以别名a3必须要有 #div 是在mysql中算出商的方式

3、算累积总计

?
1 2 3 4 5 6 7 ---- 算累积总计 ---- 表格自我连结(self join),然后将结果依序列出,算出每一行之前(包含那一行本身)的总合 select a1. name , a1.sales, sum (a2.sales) sum_total from total_sales a1,total_sales a2 where a1.sales < a2.sales or (a1.sales=a2.sales and a1. name = a2. name ) group by a1. name ,a1.sales order by a1.sales desc ;   例如: select a1.*, sum (a2.sales) sum_soales from total_sales a1,total_sales a2 where a1.sales < a2.sales or (a1.sales=a2.sales and a1. name =a2. name ) group by a1. name order by a1.sales desc ;

4、算总合百分比

?
1 2 3 4 例如: select a1.*,a1.sales/( select sum (sales) from total_sales) z_sum from total_sales a1,total_sales a2 where a1.sales < a2.sales or (a1.sales=a2.sales and a1. name =a2. name ) group by a1. name ;   # select sum (sales) from total_sales 是为了算出字段值总合,然后每一行一一除以总合,算出每行的总合百分比。

5、算累计总合百分比

?
1 2 3 4 例如: select a1. name ,a1.sales, sum (a2.sales), sum (a2.sales)/( select sum (sales) from total_sales) z from total_sales a1,total_sales a2 where a1.sales < a2.sales or (a1.sales=a2.sales and a1. name =a2. name ) group by a1. name order by a1.sales desc ;   select a1. name ,a1.sales, sum (a2.sales), truncate ( sum (a2.sales)/( select sum (sales) from total_sales),2) || '%' z from total_sales a1,total_sales a2 where a1.sales < a2.sales or (a1.sales=a2.sales and a1. name =a2. name ) group by a1. name order by a1.sales desc ;

6、空值(null)和无值(' ')的区别

1、无值的长度为0,不占用空间;而空值null 的长度是null,是占用空间的
2、is null或者is not null,是用来判断字段是不是null或者不是null,是不能查出是不是无值的
3、无值的判断使用='‘或者<>''来处理。<>代表不等于
4、在通过count()指定字段统计又多少行数时,如果遇到null值会自动忽略掉,遇到空值会自动加入记录中进行计算
?
1 2 3 4 5 6 7 8 9 例如: create table site(site varchar (20)); insert into site values ( 'nanjing' ); insert into site values ( 'beijing' ); insert into site values ( '' ); insert into site values ( 'taijin' ); insert into site values (); insert into site values ( '' ); select * from site;

?
1 2 3 4 5 select length(site) from site; select * from site where site is null ; select * from site where site is not null ; select * from site where site = '' ; select * from site where site <> '' ;

7、正则表达式(与shell部分一样)

?
1 2 3 4 5 6 7 8 9 10 11 12 匹配模式            描述                                  实例 ^               匹配文本的开始字符                       ‘^bd ' 匹配以 bd 开头的字符串 $               匹配文本的结束字符                       ‘qn$' 匹配以 qn 结尾的字符串 .               匹配任何单个字符                            ‘s.t ' 匹配任何 s 和 t 之间有一个字符的字符串 *               匹配零个或多个在它前面的字符              ‘fo*t' 匹配 t 前面有任意个 o +               匹配前面的字符 1 次或多次                  ‘hom+ ' 匹配以 ho 开头,后面至少一个m 的字符串 字符串             匹配包含指定的字符串                      ‘clo' 匹配含有 clo 的字符串 p1|p2           匹配 p1 或 p2                          ‘bg|fg ' 匹配 bg 或者 fg [...]           匹配字符集合中的任意一个字符              ‘[abc]' 匹配 a 或者 b 或者 c [^...]          匹配不在括号中的任何字符                    ‘[^ab] ' 匹配不包含 a 或者 b 的字符串 {n}             匹配前面的字符串 n 次                    ‘g{2}' 匹配含有 2 个 g 的字符串 {n,m}           匹配前面的字符串至少 n 次,至多m 次        ‘f{1,3}' 匹配 f 最少 1 次,最多 3 次

语法:select 字段 from 表名 where 字段 regexp 匹配模式

?
1 2 3 4 例如: select * from total_sales where name regexp '^[n]' ; select * from total_sales where name regexp '[n]' ; select * from total_sales where name regexp 'ho|bo' ;

8、存储过程(与shell函数差不多,代码的复用) 存储过程是一组为了完成特定功能的sql语句集合

存储过程在使用过程中是将常用或者复杂的工作预先使用sql语句写好并用一个指定的名称来进行储存,这个过程经编译和优化后存储在数据库服务器中,当需要使用该存储过程时,只需要调用它即可,存储过程在执行上比传统sql速度更快,执行效率更高。
存储过程的优点
1、执行一次后,会将生成的二进制代码驻留缓冲区,提高执行效率
2、sql语句加上控制语句的集合,灵活性高
3、在服务器端存储,客户端调用时,降低网络负载
4、可多次重复被调用,可随时修改,不影响客户端调用
5、可完成所有的数据库操作,也可控制数据库的信息访问权限
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 语法: delimiter !!                    #将语句的结束符号从分号;临时修改,以防出问题,可以自定义 create procedure xxx()      #创建存储过程,过程名自定义,()可带参数      begin                   #过程体以关键字 begin 开始      select * from xxx;      #过程体语句      end !!                   #过程体以关键字 end 结尾 delimiter ;                     #将语句的结束符号恢复为分号   call xxx;                   #调用存储过程   ====查看存储过程==== show create procedure [数据库.]储存过程名;    #查看某个储存过程的具体信息 show create procedure xxx; show procedure status [ like '%xxx%' ] g
?
1 2 3 4 5 6 7 8 9 10 11 12 例如: delimiter !! create procedure kind1() begin      select * from total_sales; end !!   delimiter ; call kind1;   show create procedure kind1g show procedure status like '%kind1%' g

存储过程的参数:
in 输入参数,表示调用者向过程传入值(传入值可以是字面量或变量)
out 输出参数:表示过程向调用者传出值(可以返回多个值,传出值只能是变量)
?
1 2 3 4 5 6 7 8 9 例如: delimiter !! create procedure kind2( in people char (20)) begin      select * from total_sales where name =people;                                                    end !!           delimiter ; call kind2( 'lisi' );

8.1、存储过程的条件语句

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 例如: delimiter !! create procedure kind7( in num int (10)) begin declare var int ; set var=num*2; if var>=10 then      update total_sales set sales=sales+1; else      update total_sales set sales=sales-1; end if; end !! delimiter ;   call kind7(5); call kind7(4);


8.2、循环语句while

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 例如: create table akg(id int );   delimiter !! create procedure kind6() begin declare var int ; set var=0; while var<5 do      insert into akg values (var);      set var=var+1; end while; end !!   delimiter ; call kind6; select * from akg;

到此这篇关于mysql高级sql语句的文章就介绍到这了,更多相关mysql高级sql语句内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://blog.csdn.net/weixin_55609814/article/details/118304246

可以去百度分享获取分享代码输入这里。
声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

【腾讯云】云服务器产品特惠热卖中
搜索
标签列表
    关注我们

    了解等多精彩内容