select单表查询,多表查询,子查询

DML:语句,经常使用的select ,insert into ,delete,updataweb

select 语句:正则表达式

select 语句通常用法为: select 字段名 from tb_name where 条件 ;svg

select 查询语句类型通常分为三种:
单表查询,多表查询,子查询函数

最简单的单表查询 : select * from tb_name;
*表示,全部字段学习

查询特定字段(投影):
select 字段名1,字段名2, from tb_name;code

where 语句过滤查询(选择)xml

select * from tb_name where 条件 ;排序

select 后面还能够跟上 关键字 distinct 表示某个字段内重复的数据只显示一次。好比,若是有个表students的字段 gender(性别),里面无非就是男或者女,咱们查看该字段,就须要显示每种类别一次就是要distinct
select distinct gender from students;
from 子句:
from 后面能够跟上一个表,多个表,或者其余select it

跟上一个表就是单表查询,跟上多个表就是多表查询,跟上select 就是嵌套查询(子查询
io

所以from 就表示咱们要查询的关系。

where 子句:

where 子句 :就是布尔关系表达式,通常 使用 > < >= <=
where 子句 数值不须要加引号, 字字符须要加引号
where 还能够跟上 逻辑 and or not
例如查询 students 表中字段age(年龄),在20-25期间的学生名字。
select Name,age from students where age >=20 and age <=25;

where 后面 跟上 like 关键词:
like :后面通常跟上 % 表示任意长度任意字符 __ 表示单个字符

例如查找students 表中 Name 字段,以 y开头全部数据,就须要使用like 匹配。
select Name from students where Name like 'y%';

查找students 表中 Name 字段,以y开头后面跟上4个字符

select Name from student where Name like 'y____'

查找students 表中 Name 字段,包含了ing 的字段。

select Name from students where Name like '%ing%';

查找students 表中 Name 字段以大写 M,N,Y开头的用户

selcet Name from students where Name like 'M%' or Name like 'N%' or Name like '%Y';

或者使用 Rlike (正则表达式)。

select Name from students where Name Rlike '^[MNY].*$';

查找students 表中年龄为 18,20,25的用户

第一种方法:
select Name from students where age=18 or age=20 or age=25;

第二种方法:使用 in 关键字,后面跟上一个列表
select Name from student where age in (18,20,25);

students 中有个字段课程(CID2)。查找出改字段,里面为空的字段的,学生姓名 Name
select Name from students where CID2 is null ;
查询不为空就是要 is not null

is null 和 is not null能够实现判断字段是否为空

order by 子句:

查询后的结果排序:

就好比咱们刚才的查询,students 中有个字段课程(CID2)。查找出改字段,里面为空的字段的,学生姓名 Name ,若是咱们想对结果进行排序。默认是升序asc 使用关键字 order by +须要排序的字段 desc (降序)

select Name from students where CID2 is null order by Name desc;

as 子句

用于给字段或者表使用别名。若是某字段名称比较长,或者表名字比较长

例如:
selcet Name as Na from students as stu;

limit 子句:

用于显示 结果的前 N 行
只显示前3行
select * from students limit 3;
只显示3行,从第5行开始计算
select * from students limit 3,5;

group by 子句 :

用于分组,好比把students 表的 学生 按照男女进行分组

select age,Gender from students group by gender

这样分出的只有两个组,男的和女的

students 表中的字段 有个课程的字段 CID 求出该字段中,须要将课程的人数,大于等于2的显示出来

having 子句

只能和group by 搭配使用,使用group by 分组以后,再用having 过滤
select CID from students where having CID >=2

多表查询 :

若是咱们有两张表 students (学生表)和 course (课程表,该表显示了,每个课程的编号(CID),名称(Cname))。如今咱们但愿查询,每一位学生,学习的第一门课程的,课程名称是什么。

select students.Name course.Cname from students ,courses where students.CID1=course.CID;
这样的查询就叫天然查询。是创建在某两个字段创建起对应关系的基础上

查询只有有同窗选修了某个课程就显示出来,若是课程没有则显示为null
这样就不能使用天然链接了,须要使用外链接

外链接分为:左外链接 left join tb_name on +链接条件
右外链接 right jion tb_name +链接条件

好比咱们查询每一个同窗选修的第一个课程名称,若是课程不存在显示null

select students.Name,course.Cname from students left join course on students.CID1=course.CID

查询哪些课程有人选,显示出同窗名称,没有人选的,显示null

select students.Name,course.Cname from students right join course on students.CID1=course.CID

子查询:(select 查询嵌套另一个select)

查找students表中,年龄大于平均年龄的同窗
select Name from students where age > (select avg(age) from students);
其中 avg :是求平均值的函数

在比较操做符中使用子查询:子查询只能返回单个词。 在 in 中使用子查询