数据查询的语法结构
select 子句1
from 子句2
[where 表达式1]
[group by 子句3]
[having 表达式2]
[order by 子句4]
[union 运算符]
[into outfile 输出文件名]
[limit [m,]n];
limit [m,] n :m是结果集中的下标,n是查询数量
1、匹配符like
通配符
- %:任意多个字符,M%,%M,%M%
- _:单个字符,_M
当匹配串不包含通配符,用=取代like,用<>取代not like
2、正则表达式
where 字段名 regexp '模式串'
3、空值查询
where 字段 is [not] null
4、连接查询
- 交叉连接
select [all | distinct] * from table1,table2
- 内连接
select A.*, B.*
from student A, sc B
where A.sno = B.sno;
或者
select A.*, B.*
from student A inner join sc B on A.sno = B.sno;
- 自连接
select A.sno
from sc A, sc B
where A.sno = B.sno and A.cno = 'C01' and B.cno = 'C04';
- 外连接:左外连接、右外连接、全外连接
select [all | distinct] *
from table1 left|right|full[outer] join table2
on table1.sno = table2.sno;
5、嵌套查询
- 不相关子查询
- 子查询返回单值:=
- 子查询返回值列表:in 、not in 、any、 some 、all
- 相关子查询:子查询依赖父查询
select sname
from student
where exists(
select *
from sc
where sno = student.sno and cno='C01';
)
6、集合查询
union intersect minus
参考资料:《MySQL数据库原理及应用》武洪萍