数据库的单标查询 DQL

DQL: 查询表中的记录

1)语法:
	select * from 表名;

	select 
		字段名列表
	from
		表名列表
	where
		条件列表
	group by
		分组条件
	order by
		排序
	limit
		分页限定

2)基础查询
	(1)多个字段的查询
		select name, age from student;
		select * from student;  --不方便阅读,一般全部名字写出来


		select 
			name, 	 --姓名
			age, 	 --年龄
		from 
			student; --学生表

	(2)去除重复
		select distinct address from student;  --要去重,必须保证结果集一样
		select distinct name, address from student; --去重不掉,因为结果集不完全相同

		
	(3)计算列
		select name, math, english, math+english from student; -- math和英语之和

	(4)起别名
		select name, math, english, math+ ifnull(english, 0) as 总分 from student; -- math和英语之和

3)条件查询
	(1)where字句后跟条件
		select * from student where age > 20;
		select * from student where age >= 20;

		select * from student where age = 20;

		select * from student where age != 20;

		select * from student where age <> 20;

		select * from student where age >= 20 && age <= 30;
		select * from student where age >= 20 and age <= 30;

		select * from student where age = 20 or age = 30;
		select * from student where age in(22,18,25);

		注意: select * from student where english = null; -- 是不对的,查询不出来
		select * from student where english is null; --查询缺考的
		select * from student where english is not null; --不为null的

		select * from student where name like '马%'; 
		select * from student where name like '_化%'; 
		select * from student where name like '___%'; 
		select * from student where name like '%马%'; 
			_: 单个字符
			%:多个字符

	(2)运算符
		> < <= >= = <>
		between ... and
		in(集合)
		like
		is null
		and 或 &&
		or 或 &&
		not 或 !

4)查询语句
	(1)排序查询 order by 子句;
		order by 排序字段1 排序方式1 排序字段2 排序方式2...;
			如: 
				默认是升序排列: select * from order by math;
				升序: select * from order by math asc;
				降序: select * from order by math desc;

				先按数学排名,如果数学一样,则按照英语排名(第2排序):	select * from order by math asc, english asc;

	(2)聚合函数:将一列的数据作为一个整体,进行纵向的计算
		count: 计算个数
			select count(name) from student; -- 查询有几个学生. 单行单列。 排除了null的值了
			select count(isnull(english, 0)) from student; -- 如果是null,则被替换为0

			select count(*) from student;
		max: 计算最大值
			select MAX(math) from student;

		min: 计算最小值
			select min(math) from student;

		sum: 求和
			select sum(math) from student;

		avg: 计算平均值
			select avg(math) from student;

	(3)分组查询(每一组的平均分更高一点,把一组的同学当成一个整体来看,把相同属性的当成一个组来看待)
		如: 按照男、女来分为2组计算下平均分:
			select sex, avg(math), count(id) from student group by sex;
				女 91 3
				男 72 5

		注意:分组之后,除了分组的字段,不要加其他字段了
			如:select name, sex, avg(math), count(id) from student group by sex; -- 这个name就不要加,没意义

		分数低于70分的不参与分组(认为不是这个班的学生): select sex, avg(math), count(id) from student where math > 70 group by sex;

		分组前对条件做一些限定,如: 邀请人数要大于2人
			select sex, avg(math), count(id) from student where math > 70 group by sex having count(id) > 2;
			优化写法(起别名,拿别名判断):
				select sex, avg(math), count(id) 人数 from student where math > 70 group by sex having 人数 > 2;

		面试: where和having的区别:
			where在分组之前进行限定,如果不满足条件,则不参与分组;
			having在分组之后限定,如果不满足结果,则不会被查询出来;

	(4)分页查询(查询结果有300W个,就可以搞几页)
		语法: limit 开始的索引,每页查询的条数

		例子: 每页显示3条记录
			select * from student limit 0, 3; -- 从0开始查询3条记录 第1页 
			select * from student limit 3, 3; -- 从0开始查询3条记录 第2页

		公式: 开始索引 = (当前页码-1) * 每页显示的条数
			select * from stu limit (当前页码-1) * 每页显示的条数, 每页显示的条数

		分页操作,是一个方言。 limit只能在mysql中用,在oracle等都有自己各自的写法.


5)约束

6)多表之间的关系

7)范式: 抽取表的设计规则, 

8)数据库的备份与还原

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章