MySql 高级查询 select 完整的查询语法组成

select [选项 all|distinct]
字段表达式
from 子句
where 子句
group by 子句
having 子句
order by 子句
limit 子句;

sql完整的查询分为8个部分, 每个部分要么不写,要么必须写在正确的顺序上.

1. select [选项 all|distinct]

2. 字段表达式: 表示从数据源中查询哪些字段

写法:
a. 单独列举字段名
select name,sex from student;

b. * : 通配符,匹配所有的字段名
select * from student;

c. 聚合函数: count(),sum(),avg(),max(),min(),group_concat()
# 查询学生人数
select count(*) from student;

d. 为字段取别名: 字段名 as 别名
select count(*) as count from student;
select count(*) count from student; # as 可以省略

3. from 子句

from 后面接的是数据源, 表示查询数据的来源.
	
数据源: 通常由 一张表,或多张表(指连表查询), 或子查询 组成.

连表查询: 查询学生的姓名及其班级名和教室
select student.name,class.class_name,class.class_room
from student 
join class on student.class_id=class.class_id;

4. where 子句

语法: where 条件表达式
作用: 使用用于对数据源进行过滤和筛选. 
过滤和筛选原理: 根据 "条件表达式" 的结果, 如果结果为True,数据被保留;如果结果为False结果被
过滤调用;

例如:
	select * from student where sex="女";
	select * from student where 1; # 查询所有数据
条件表达式的写法:

4.1. 比较运算

> >= < <=  
特殊: =    !=  <> (也是不等于)

查询性别不为男的学生:
select * from student where sex != "男";
select * from student where sex <> "男";

4.2. 集合判断 in 和 not in

语法: 字段 in (1,2,3....)
语法: 字段 not in (1,2,3....)

例子: 查询学生id为 1,3,6,8,10 的学生的信息
select * from student where id in (1,3,6,8,10);
例子: 删除学生id为 1,3,6,8,10 的学生的信息
delete from student_copy where id in (1,3,6,8,10);

4.3. 模糊查询 : like

语法: 字段名 like "%_关键字"

例子: 查询姓张的学生
select * from student where name like "张%"

例子: 查询姓猪的学生,名字由三个字组成
select * from student where name like "猪__";

4.4. 范围查询: between … and …

语法: 字段名 between 小值 and 大值  , 包含边界值
语法: 字段名 not between 小值 and 大值 , 不包含边界值

例子: 查询年龄在 30-49 之间的学生
select * from student where age between 30 and 49;

例子: 查询年龄不在 30-49 之间的学生
select * from student where age not between 30 and 49; 

4.5. is null 和 is not null : 判断字段值是否为null

在MYSQL中,null与任意值进行比对,结果为False,甚至与自己比较都是False.

只能通过: is nullis not null 判断字段值是否为 null;

例子: 查询班级id大于等于2的学生的人数 和 小于2的学生的人数
		select count(*) as count from student where class_id >= 2;
		select count(*) as count from student where class_id < 2;
		select count(*) as count from student where class_id is null;

4. 6. 逻辑运算

与: and
或:  or 
非: not

例如: 查询年龄大于30的老男人;
select * from student where age > 30 and sex = "男";
例如: 查询年龄小30的或者为女性, 年轻人;
select * from student where age < 30 or sex = "女";

5. group by 分组查询:

语法: group by 字段
分组的目的: 为了统计结果
	结合统计函数:
	count()
	sum()
	avg()
	max()
	min()
	group_concat() : 将组内字段的值通过,拼接成一个字符串

总计: 分组查询只能查找聚合函数以及分组的字段

a. 例子: 求每个班级的人数, 每个班平均年龄,统计每个班的名单
select class_id,count(*) as count,avg(age) as avg, group_concat(name) as 名单
from student 
where class_id is not null
group by class_id

b. 例子: 求男女分别的人数
select sex,count(*) as count
from student
group by sex;

6. having 子句:

用于分组后的再过滤(用法和where一样,只是位置不同)
	语法: having 条件表达式

a, 例子: 查询班级人数大于2的班级id
	select class_id,count(*) as count
	from student
	where class_id is not null
	group by class_id
	having count > 2;

7. order by 子句: 排序, 对数据源进行排序

语法:
		order by 字段 [规则 asc|desc], 字段2 [规则];

说明: 
	1. asc 默认 升序|desc 降序
	2. 按照多个字段排序,先排好第一个,在此基础上的分组的部再按第二个字段排序.

a. 查询学生信息,要求按照年龄降序排列
	select * from student order by age desc;
b. 查询学生信息,要求按照班级id升序排列,再按照年龄降序排列;
	select * from student where class_id is not null order by class_id asc,age desc;

8. limit 子句: 限制查询结果的条数,作用是减轻数据库服务器的压力.

语法: limit start,length;
表示 从开始索引start(默认0)位置查起,总共查length条记录

应用: 分页

查询学生信息: 每页显4条
第一页: 
select * from student limit 0,4;
第二页:
select * from student limit 4,4;
第三页:
select * from student limit 8,4;
第n页:
select * from student limit (n-1)*4,4;

猜你喜欢

转载自blog.csdn.net/qq_39286483/article/details/103906633