文章目录
1 SQL高级语句
1.1 select
显示表格中的一个或者多个字段中所有的信息
select 字段名 from 表名;
示例1:
select * from qq;
select * from qq;
1.2 distinct
查询不重复记录
#语法:
select distinct 字段 from 表名﹔
示例1:去除年龄字段中重复的
select distinct age from qq;
1.3 where
where 有条件的查询
#语法:
select '字段' from 表名 where 条件
#示例:显示name和age 并且要找到age小于20
select name,age from qq where age < 95;
1.4 and ;or
#语法:
select 字段名 from 表名 where 条件1 (and|or) 条件2 (and|or)条件3;
示例1:显示name和age 并且要找到age大于20小于30
select name,age from qq where age >94 and age <100;
1.5 in
显示已知值的资料
语法:
select 字段名 from 表名 where 字段 in ('值1','值2'....);
#示例1:显示学号为1,2,3,4的学生记录
select * from jianlai where prow in (55,90,100);
1.6 between
显示两个值范围内的资料
#语法:
select 字段名 from 表名 where 字段 between '值1' and '值2';
包括 and两边的值
#示例1:显示学生姓名在Ding Dian和Hua Rong中的学生记录
select * from jianlai where name between 90 and 100;
2 like 通配符
通配符通常是和 like 一起使用
#语法:
select 字段名 from 表名 where 字段 like 模式
#示例1:查找名字以道开头的记录
select * from jianlai where name like '道%';
3 order by
order by 按关键字排序
#语法:
select 字段名 from 表名 where 条件 order by 字段 [asc,desc];
asc :正向排序
desc :反向排序
默认是正向排序
select age,name from jianlai order by age;
反向排序
select age,name from jianlai order by age desc;
``#示例3:显示name、age和address字段的数据 并且只显示address字段为文庙的 并且以age字段排序
select age,name,address from jianlai where address=文庙 order by age;
4 函数
4.1 数学函数
#示例1:返回-2的绝对值
select abs(-2);
#示例2:随机生成一个数
select rand (1);
#示例3:随机生成排序
select * from jianlai 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最近的数
4.2 聚合函数
#示例1:求表中年龄的平均值
select avg(age) from jianlai;
#示例2:求表中年龄的总和
select sum(age) from jianlai;
#示例3:求表中年龄的最大值
select max(age) from jianlai;
4.3 字符串函数
length
#语法:
select length(字段) from 表名;
#示例:计算出字段中记录的字符长度
select name,length(name) from jianlai;
replace
#语法:
select replace(字段,'原字符''替换字符') from 表名;
select replace(name,'道','wu') from jianlai;
concat
#语法:
select concat(字段1,字段2)from 表名
#示例1:将name,prow字段拼接成一个字符串
select concat(name,prow) from jianlai;
5 group by
- 对group by 后面的字段的查询结果进行汇总分组,通常是结合聚合函数一起使用的
- group by 有一个原则,就是select 后面的所有列中,没有使用聚合函数的列必须出现在 group by 的后面。
#语法:
select 字段1,sum(字段2) from 表名 group by 字段1;
#示例1:求各个prow的年龄总和
select prow,sum(age) from jianlai group by prow;
6 连接查询
6.1 inner join(等值相连)
只返回两个表中联结字段相等的行
SELECT * FROM jianlai A INNER JOIN score B on A.id = B.id;
6.2 left join (左联接)
返回包括左表中的所有记录和右表中联结字段相等的记录
select * from score A left join jianlai B on A.id = B.id;
6.3 right join (右联接)
select * from score A right join jianlai B on A.id = B.id;
7 子查询
连接表格,在WHERE 子句或HAVING 子句中插入另一个SQL语句
语法:
SELECT "栏位1" FROM "表格1" WHERE "栏位2" [比较运算符]
#外查询
(SELECT "栏位1" FROM "表格1" WHERE "条件");
#示例:查询id为1的得分总和
select sum(score) from scores where stuid in (select stuid from jianlai where stuid=1);
8 exists
- 用来测试内查询有没有产生任何结果类似布尔值是否为真
- 如果有的话,系统就会执行外查询中的SQL语句。若是没有的话,那整个 SQL 语句就不会产生任何结果。
#语法:
SELECT "栏位1" FROM "表格1" WHERE EXISTS (SELECT * FROM "表格2" WHERE "条件");
示例1:先看jianlai表中是否有id为1的学生,如果有则执行将score表中的score求和
select sum(score) from score where exists (select * from jianlai where id=1);
#示例2:先看jianlai表中是否有id为88的,如果有则执行将scores表中的score求和
select sum(score) from score where exists (select * from jianlai where stuid=88);