1.基本查询
-
查询指定列的数据:
select 字段1,字段2,字段3,... from 表名 where 条件语句;
-
定义别名:
select 字段1 别名1,字段2 别名2,字段3 别名3,... from 表名 where 条件语句;
select 字段1 as 别名1,字段2 as 别名2,字段3 as 别名3,... from 表名 where 条件语句;
-
消除重复:
select distinct 字段名 from 表明 where 条件语句;
-
限定结果集的行数:
select top5 字段1,字段2,字段3,... from 表名 where 条件语句; //结果集的前5行
select top10 percent 字段1,字段2,字段3,... from 表名 where 条件语句; //结果集的前10%行
-
计算列的值:
select sum1,sum2,sum1+sum2 as sum from 表名 [where clause];/ /计算并显示两数和
-
聚合函数:
函数 | 功能 |
---|---|
avg(<字段表达式>) | 求一列数据的平均值 |
sum(<字段表达式>) | 求一列数据的和 |
min(<字段表达式>) | 求列中数据的最小值 |
max(<字段表达式>) | 求列中数据的最大值 |
count(* I 字段名) | 统计查询的行数 |
创建图表:
2. 条件查询
- 用于where子句的运算符
运算符 | 说明 | 运算符 | 说明 |
---|---|---|---|
= | 等于 | <>或!= | 不等于 |
< | 小于 | <= | 小于等于 |
> | 大于 | >= | 大于等于 |
is null | 为空 | is not null | 不为空 |
and | 并且 | or | 或者 |
in | 包含 | not in | 不包含 |
between…and… | 两者之间 | not | 用来取非 |
like | 模糊查询(%:任意 _:一个 ) | -------------------- | -------------------------------- |
-
算术运算查询:
select 字段1,字段2,字段3 frome 表名 where 条件表达式;
例:select * from t_student where point>800; -
带in关键字查询:
select 字段1,字段2 frome 表名 where 字段 [not] in(元素1,元素2);
例:select * from score where final in (60,80); -
带between and的范围查询:
select 字段1,字段2 frome 表名 where 字段 [not]between 取值1 and 取值2;
例:select * frome score where final between 60 and 80; -
模糊查询:
select 字段1,字段2… frome 表名 where 字段 [not] like ‘字符串’;
“%”代表任意字符; “_”代表单个字符;
例:select * frome student where sname like ‘梁%”; -
空值查询:
select 字段1,字段2…frome 表名 where 字段 is[not] null;
例:select * frome student where email is null; -
多条件查询(and):
select 字段1,字段2…frome 表名 where 条件表达式1 and 条件表达式2 [and 条件表达式n]
例:select * frome student where classno=‘080601’ and point>700; -
多条件查询(or)
select 字段1,字段2…frome 表名 where 条件表达式1 or 条件表达式2 [or 条件表达式n]
例:select * from student where classno=‘080501’ or classno=‘080601’; -
对查询结果排序order by:
select 字段1,字段2…from 表名 order by 属性名 [asc|desc]
例:select * frome student order by point desc;//降序,从大到小 -
分组查询group by
select 字段1, 字段2,...,聚合函数
from 表明
group by 字段名
[having 条件语句];
例:select studentno,courseno,avg(final) from score group by studentno having avg(final)>90; -
分页查询(limit):
select 字段1,字段2,…from 表名 limit 初始位置,记录数;
例:select * from student limit 0,4; -
合并查询(union | union all)
select id from student
union
select id from score;
3.连接查询
select st.studentno,st.sname,st.classno,cl.department
from student st,class cl
where st.classno=cl.classno;
1.内连接查询(INNER)
-
select st.studentno,st.sname,st.classno,cl.department
from student st inner join class cl
on st.classno=cl.classno;
-
select st.studentno,st.sname,st.classno,sc.courseno,co.cname
from student st join score sc
on st.studentno=sc.studentno
join course co
on sc.courseno=co.courseno
where st.sname='韩吟秋';
2.外连接查询OUTER(两张或以上的表连接起来查询某张表的信息)
-
左连接查询
select *
from course co left join score sc
on co.courseno=sc.courseno;
-
右连接查询
select *
from course co right join score sc
on co.courseno=sc.courseno;
4. 嵌套查询
-
普通嵌套查询
select * from score sc
where sc.studentno=(select studentno from student where sname='韩吟秋');
-
多值嵌套(ANY,ALL,IN,EXISTS)
-
带in关键字的嵌套查询
//查询与“何影”同班的学生学号、姓名和电话(使用子查询)
select s.studentno,s.sname,s.phone
from student s
where s.classno in(
select classno from student where sname ='何影') and s.sname!='何影';
-
带any关键字的嵌套查询(any关键字表示满足其中任一条件)
select * from student s where point> any(select point from student);
-
带all关键字的嵌套查询(all关键字表示满足所有条件)
select * from student s where point>= all(select point from student);
-
带exists关键字的嵌套查询
select * from student st
where not exists(select * from score sc where sc.courseno='c05109' and st.studentno=sc.studentno);
-