我是靠谱客的博主 飞快电话,这篇文章主要介绍mysql学习笔记(五) 数据库表的查询基本操作数据库表的查询基本操作二.多表查询:,现在分享给大家,希望可以做个参考。

数据库表的查询基本操作

DQL(Data Query Language): 查询操作。

一.单表查询:

一.普通查询

复制代码
1
2
3
4
5
6
7
8
9
10
--查询student表中的所有数据 select *from student --查询student表中指定字段 select id, name from student --查询student表中指定数据 select id,name from student where id=2 ; select id,name from student where age=18 and sex="famale" ;

二.模糊查询

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
1、% 替代一个或多个字符 2、_ 仅替代一个字符 3、[charlist] 字符列中的任何单一字符 --查询以住在以'Ne'开始城市的人 select *from persons where City like 'Ne%' ; --查询居住在包含on城市的人 select *from persons where City like '%on%' ; --查询城市第一个字符后为'dodon'的城市 select *from persons where City like '_ondon'l; --查询居住的城市以 "A" 或 "L" 或 "N" 开头的人: select *from persons where City like '[ALN]%' ;

三:distinct去重

复制代码
1
2
3
4
5
--去除age中相同的数据 select distinct age from student; --去除age和sex都相同的数据 select distinct age,sex from student;

四.limit子句

复制代码
1
2
3
4
5
6
7
--检索前4行数据 显示id=1,2,3,4的数据 select *from test limit 0,3; select *from test limit 3; -- 检索3行数据 显示id=4,5,6的数据 select *from test limit 3,5;

五.group by 和having子句:

复制代码
1
2
3
4
5
6
7
--按sex分组 统计人数并显示sex个数 select count(*) from group by sex; --按sex分组 统计人数并显示sex个数和具体的sex select sex,count(*) from group by sex;

where,having,和group by联合使用

在最初学习group by的时候,我就陷入了一个误区,那就是group by不能和where一起使用,只能用having……

其实它们都是可以一起使用的,只不过是where只能在group by 的前面,having只能在group by 的后面。

滤清执行顺序:①先对*进行筛选,②对筛选的结果进行分组,③对分组的结果进行筛选

复制代码
1
2
3
4
5
6
7
8
--30100学院的每个专业的学生有多少人。 select majar, count(*) from student where college = 30100 group by majar ; --查询每个学院的学生有多少人,并且只要学生人数大于3的。 select college,count(*) from student group by college having count(*) >3 ;

六.order by子句

复制代码
1
2
3
4
5
6
7
--student表中id按逆序排列 id=5,4,3,2,1 select *from student order by id desc --student表中id按顺序排列 id=1,2,3,4,5 select *from student order by id asc --student表中按字母(是英语非中文)顺序显示名称 select *from student order by name asc;

七:聚合函数(常用六个)

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
--count(*) 统计个数 select count(*) from student; --和count(*)一样统计个数,但是不同的是它除去age中为null的行数 select count(age) from student; --将age中数值相加 select sum(age) from student; --将age中数值相加求平均值 select avg(age) from student; --求age中最大值 select MAX(age) from student; --将age中最小值 select MIN(age) from student;

二.多表查询:

1.   连接查询(交叉连接,内连接,左外连接,右外连接)

  •  交叉连接(笛卡尔积原理)
复制代码
1
2
3
4
5
--student表全部内容和mark表全部内容生成笛卡儿积 select *from student,mark; --student表id,name和mark表grade生成笛卡儿积 select student.id ,studen.name ,mark.grade from student ,mark;

 

  •  内连接:用的最多
复制代码
1
2
3
4
5
6
7
--student表内连接mark表查看student的id,student的name,mark的grade当满足mark.stu_id=student.id; select student.id ,student.name ,mark.grade from student inner join mark on mark.stu_id=student.id where ...条件 ; <=>select student.id ,student.name ,mark.grade from student inner join mark where mark.stu_id=student.id; <=> select student.id ,student.name ,mark.grade from student ,mark where mark.stu_id=student.id;
  •  左外连接:优先显示左表全部记录 ,保留左边的记录,相比内连接可以匹配右边的null
复制代码
1
insert student.id ,student.name,mark.grade from student left join mark on studen.id=mark.stu_id;
  •  右外连接: 优先显示右表全部记录 ,保留右边的记录,相比内连接可以匹配左边的null   
复制代码
1
insert student.id ,student.name,mark.grade from student right join mark on studen.id=mark.stu_id;

2.   联合查询:取的列数数量一致

复制代码
1
2
3
4
5
//将name和grade放同一列 select name from student union all select grade from mark; //将name和grade放同一列,将student的id和mark表中的id放同一列 //select name,id from student union all select grade,id from mark;

 

3. 嵌套查询(子查询)

复制代码
1
2
--从mark表中获取id号,根据该id号用来查询student中的数据 select *from student where id in (select stu_id from mark);

 

最后

以上就是飞快电话最近收集整理的关于mysql学习笔记(五) 数据库表的查询基本操作数据库表的查询基本操作二.多表查询:的全部内容,更多相关mysql学习笔记(五)内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(80)

评论列表共有 0 条评论

立即
投稿
返回
顶部