MySQL数据库中的高级(进阶) SQL 语句
作者:互联网
一、SQL高级语句
1.1 SELECT
显示表格中一个或数个栏位的所有资料
语法:SELECT "栏位" FROM "表名";
实例:查询表中的name一列
select "name" from class1;
1.2 DISTINCT
不显示重复的资料
语法:SELECT DISTINCT "栏位" FROM "表名"; SELECT DISTINCT Store_Name FROM Store_Info;
1.3 WHERE
有条件查询
语法:SELECT "栏位" FROM "表名" WHERE "条件";
实例:
select name from football where value > 60000000;
1.4 AND OR
代表且、或、和的意思
语法:SELECT "栏位" FROM "表名" WHERE "条件1" {[AND|OR] "条件2"}+ ;
示例:
select name from football where value > 60000000 or (value >30000000 and value < 60000000);
1.5 IN ----显示已知的值的资料
语法:SELECT "栏位" FROM "表名" WHERE "栏位" IN ('值1', '值2', ...);
实例:
select name from football where name in ('梅西','C罗');
1.6 BETWEEN
显示两个值范围内的资料
语法:SELECT "栏位" FROM "表名" WHERE "栏位" BETWEEN '值1' AND '值2';
示例:
select name from football where age between '20' and '30';
1.7 通配符
通常通配符都是跟 LIKE 一起使用的
1.8 LIKE
匹配一个模式来找出我们要的资料
语法:SELECT "栏位" FROM "表名" WHERE "栏位" LIKE {模式};
示例1:
select name from football where value like '8%';
示例2:
select name from football where age like '2_';
示例3:
select name from football where value like '%000';
1.9 ORDER BY
按关键字排序
语法:SELECT "栏位" FROM "表名" [WHERE "条件"] ORDER BY "栏位" [ASC, DESC];
#ASC 是按照升序进行排序的,是默认的排序方式。
#DESC 是按降序方式进行排序。
实例1:
select name,value from football order by value desc;
实例2:
select name,age,value from football order by age asc;
二、SQL函数
2.1 数学函数:
select abs(-10),rand(),mod(10,3),power(2,10),round(3.1415);
select round(3.1415926,3),sqrt(9),truncate(3.1415926,3),ceil(3.5),floor(1.11),greatest(1,2,3,4),least(1,2,3,4);
2.2 聚合函数:
实例1:计算平均身价
select avg(value) from football;
实例2:计算身价的非null个数
select count(value) from football where value is not null;
select count(distinct value) from football;
select count(*) from football;
实例3:
select max(value) from football; #最高值
select min(value) from football; #最低值
select sum(value) from football; #总和
2.3 字符串函数:
实例1:trim()
select trim(' abc ');
实例2:concat(x,y)
select concat(name,value) from football where id=1;
select name || value from football where id=1;
#如sql_mode开启 了PIPES_AS_CONCAT, "||" 视为字符串的连接操作符而非或运算符,和字符串的拼接函数Concat相类似,这和Oracle数据库使用方法一-
可以添加分隔符
select name || ':' || value from football where id=1;
实例3:substr(x,y) 和 substr(x,y,z)
select substr(value,4) from football where name='内马尔'; #value的第4位开始的字符
select substr(value,1,2) from football where name='内马尔'; #value的第1位开始的2位字符
实例4:length(x)
select length(value) from football where id =5;
select name,value,length(value) from football; # 显示value的长度并显示name和value
实例5:replace(x,y,z)
select name,value,replace(value,0,9) from football;
实例6:upper(x) 和 lower(x)
select name,upper(name) from football where id=7;
select name,lower(name) from football where id=7;
实例7:left(x,y)和 right(x,y)
select name,left(name,5) from football where id=7;
select name,right(name,3) from football where id=7;
实例8:repeat(x,y)
select id,name,repeat(id,5) from football where id=7;
实例9:space(x)、strcmp(x,y)和reverse(x)
2.4 GROUP BY
对GROUP BY后面的栏位的查询结果进行汇总分组,通常是结合聚合函数一起使用的 GROUP BY 有一个原则,就是 SELECT
后面的所有列中,没有使用聚合函数的列,必须出现在GROUP BY后面。
语法:SELECT "栏位1", SUM("栏位2") FROM "表名" GROUP BY "栏位1";
示例:
select name,sum(value) from football group by name;
select name,sum(value) from football group by name order by value desc;
2.5 HAVING
用来过滤由GROUP BY语句返回的记录集,通常与GROUP BY语句联合使用
HAVING语句的存在弥补了WHERE关键字不能与聚合函数联合使用的不足。如果被SELECT的只有函数栏,那就不需要GROUP BY子句。
语法:SELECT "栏位1", SUM("栏位2") FROM "表格名" GROUP BY "栏位1" HAVING (函数条件);
示例:
select name,sum(value) from football group by name having sum(value) > 80000000;
2.6 别名
栏位別名 表格別名
语法:SELECT "表格別名"."栏位1" [AS] "栏位別名" FROM "表格名" [AS] "表格別名";
示例:
select f.area,sum(value) "total value" from football f group by f.area;
2.7 连接查询
建立两张表,字段数据如下,后面测试用:
1)inner join(等值相连)
只返回两个表中联结字段相等的行
SELECT * FROM club A INNER JOIN football B on A.name = B.name;
2)left join(左联接)
返回包括左表中的所有记录和右表中联结字段相等的记录
SELECT * FROM club A LEFT JOIN football B on A.name = B.name;
3)right join(右联接)
返回包括右表中的所有记录和左表中联结字段相等的记录
SELECT * FROM club A RIGHT JOIN football B on A.name = B.name;
2.8 子查询
连接表格,在WHERE 子句或HAVING 子句中插入另一个SQL语句
语法:SELECT "栏位1" FROM "表格1" WHERE "栏位2" [比较运算符] #外查询
(SELECT "栏位1" FROM "表格1" WHERE "条件"); #内查询
示例:
SELECT SUM(value) FROM football WHERE name IN (SELECT name FROM club WHERE club = '皇家马德里');
标签:栏位,football,进阶,MySQL,value,SELECT,SQL,select,name 来源: https://www.cnblogs.com/ly159357/p/15216370.html