目录
10.3left join(左联接)
11.子查询
12.EXISTS
一、SQL高级语句
1.导入文件至数据库
#将脚本导入 source 加文件路径
mysql> source /backup/yzx.sql;
需要先进入数据库再导入
2. select
显示表格中的一个或者多个字段中所有的信息
#语法:
select 字段名 from 表名;
示例1:
select * from stu;
示例2
select name from stu;
3. distinct
#语法:
select distinct 字段 from 表名﹔
4.where
where 有条件的查询
#语法:
select '字段' from 表名 where 条件
#示例:显示name,id并且要找到moeny小于1000
select name,id from stu where money< 1000;
5.and;or
#语法:
select 字段名 from 表名 where 条件1 (and|or) 条件2 (and|or)条件3;
示例1:显示name和id 并且要找到money大于1000小于2000
select name,id from stu where money >1000 and money <2000;
6.in
显示已知值的资料
#语法:
select 字段名 from 表名 where 字段 in ('值1','值2'....);
#示例1:显示学号为1,2,3,4的学生记录
select * from stu where id in (1,2,3,4);
7.between
显示两个值范围内的资料
#语法:
select 字段名 from 表名 where 字段 between '值1' and '值2';
包括 and两边的值
#示例1:显示id在1-3之间的学生记录
select * from stu where id between 1 and 3;
8.like 通配符
通配符通常是和 like 一起使用
#语法:
select 字段名 from 表名 where 字段 like 模式
#示例1:查找名字以s开头的学生记录
select * from stu where name like 'y%';
#示例2:查找名字包含aa的学生记录
select * from stu where name like '%aa%';
#示例3:查找名字第二个字母为z,第三个字母为x的学生记录
select * from students where name like '_zx%';
9. order by
order by 按关键字排序
#语法:
select 字段名 from 表名 where 条件 order by 字段 [asc,desc];
asc :正向排序
desc :反向排序
默认是正向排序
#示例1:按学生的money正向排序显示姓名字段
select name from stu order by money;
#示例1:按学生的money反向排序显示姓名字段
select name from stu order by money desc;
10.函数
10.1数学函数
#示例1:返回-2的绝对值
select abs(-2);
#示例2:随机生成一个数
select rand (1);
#示例3:随机生成排序
select * from stu order by rand();
#示例4:返回7除以2以后的余数
select mod(7,2);
#示例5:返回2的3次方
select power(2,3);
#示例6:返回离2.6最近的整数
select round(2.6);
返回离2.4最近的整数
select round(2.4);
#示例7:保留3.14159的2位小数并且四舍五入
select round(3.14159,2);
保留3.14159的3位小数并且四舍五入
select round(3.14159,3);
#示例8:返回数字3.14159 截断为2位小数的值
select truncate(3.14159,2);
#示例9:返回大于或等于3.14159 的最小整数
select ceil(3.14159);
#示例9:返回小于或等于3.14159 的最大整数
select floor(3.14159);
#示例11:返回集合中最大的值
select greatest(1,4,3,9,20);
#示例12:返回集合中最小的值
select least(1,4,3,9,20);
10.2 聚合函数
#示例1:求表中mmoney的平均值
select avg(money) from stu;
#示例2:求表中money的总和
select sum(money) from stu;
#示例3:求表中money的最大值
select max(money) from stu;
#示例4:求表中money的最小值
select min(money) from stu;
#示例5:求表中有多少money记录
select count(money) from stu;
count(明确字段):不算空记录
#示例5:求表中有多少记录
select count(*) from stu;
count(*):算空记录
PS:空格空字段会识别
10.3字符串函数
trim
语法:
select trim (位置 要移除的字符串 from 字符串)
其中位置的值可以是
leading(开始)
trailing(结尾)
both(起头及结尾)
#区分大小写
要移除的字符串:从字符串的起头、结尾或起头及结尾移除的字符串,缺省时为空格。
#示例1:从名字开头的开始,移除yzx中的y显示
select trim(leading 'y' from 'yzx');
#示例2:去除空格
select trim(both from ' zhang san ');
length
#语法:
select length(字段) from 表名;
#示例:计算出字段中记录的字符长度
select name,length(name) from stu;
replace
#语法:
select replace(字段,'原字符''替换字符') from 表名;
select replace(name,'aa','god') from stu;
concat
#语法:
select concat(字段1,字段2)from 表名
#示例1:将name,id字段拼接成一个字符串
select concat(name,id) from stu;
#示例2:只显示money为2000的名字和id为一个组合记录
select concat(name,id) from stu where money=2000;
#示例3:中间加制表符
select concat(name,'\t',id) from stu where money=5000;
substr
#语法:
select substr(字段,开始截取字符,截取的长度) where 字段='截取的字符串'
#示例1:截取第3个字符往后
select substr(name,3) from stu where name='leng xuetong';
#示例2:截取第3个字符往后2个字符
select substr(name,3,2) from stu where name='leng xuetong';
group by
-
对group by 后面的字段的查询结果进行汇总分组,通常是结合聚合函数一起使用的
-
group by 有一个原则,就是select 后面的所有列中,没有使用聚合函数的列必须出现在 group by 的后面。
#语法:
select 字段1,sum(字段2) from 表名 group by 字段1;
#示例1:求各个班的money总和
select id,sum(money) from stu group by id;
#示例2:求各个班的平均年龄
select classid,avg(age) from students group by classid;
#示例3:根据年龄查看每个班的人数
select classid,count(age) from students group by classid;
having
-
having:用来过滤由group by语句返回的记录集,通常与group by语句联合使用
-
having语句的存在弥补了where关键字不能与聚合函数联合使用的不足。如果被SELECT的只有函数栏,那就不需要GROUP BY子句。
-
要根据新表中的字段,来指定条件
#语法:
SELECT 字段1,SUM("字段")FROM 表格名 GROUP BY 字段1 having(函数条件);
#示例:查看money在2000以上的人的id
select id,money from stu group by id having money > 2000;
别名
栏位別名 表格別名
v#语法:
SELECT "表格別名"."栏位1" [AS] "栏位別名" FROM "表格名" [AS] "表格別名";
#示例:设置表名别名为f,基于班级号来统计各班年龄总和,sum(age)定义别名为total age
select f.classid,sum(age) 'total age' from students as f group by f.classid;
连接查询
连接表格,在WHERE 子句或HAVING 子句中插入另一个SQL语句
inner join(等值相连)
只返回两个表中联结字段相等的行
SELECT * FROM student A INNER JOIN score B on A.stuid = B.stuid;
left join(左联接)
返回包括左表中的所有记录和右表中联结字段相等的记录
select * from score A left join student B on A.stuid = B.stuid;
右表中没有的用NULL填充
right join(右联接)
select * from score A right join student B on A.stuid = B.stuid;
左表没有的用NULL填充
子查询
连接表格,在WHERE 子句或HAVING 子句中插入另一个SQL语句
语法:
SELECT "栏位1" FROM "表格1" WHERE "栏位2" [比较运算符]
#外查询
(SELECT "栏位1" FROM "表格1" WHERE "条件");
#示例:查询学生学号为1的得分总和
select sum(score) from score where stuid in (select stuid from student where stuid=1);
EXISTS
-
用来测试内查询有没有产生任何结果类似布尔值是否为真
-
如果有的话,系统就会执行外查询中的SQL语句。若是没有的话,那整个 SQL 语句就不会产生任何结果。
#语法:
SELECT "栏位1" FROM "表格1" WHERE EXISTS (SELECT * FROM "表格2" WHERE "条件");
#示例1:先看students表中是否有stuid为1的学生,如果有则执行将scores表中的score求和
select sum(score) from score where exists (select * from student where stuid=1);
#示例2:先看student表中是否有stuid为88的学生,如果有则执行将scores表中的score求和
select sum(score) from score where exists (select * from student where stuid=88);