600字范文,内容丰富有趣,生活中的好帮手!
600字范文 > mysql学习笔记(五) 数据库表的查询基本操作

mysql学习笔记(五) 数据库表的查询基本操作

时间:2023-10-14 17:10:56

相关推荐

mysql学习笔记(五) 数据库表的查询基本操作

数据库表的查询基本操作

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

一.单表查询:

一.普通查询

--查询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、[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去重

--去除age中相同的数据select distinct age from student;--去除age和sex都相同的数据select distinct age,sex from student;

四.limit子句

--检索前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子句:

--按sex分组 统计人数并显示sex个数select count(*) from group by sex;--按sex分组 统计人数并显示sex个数和具体的sexselect sex,count(*) from group by sex;

where,having,和group by联合使用

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

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

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

--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子句

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

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

--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. 连接查询(交叉连接,内连接,左外连接,右外连接)

交叉连接(笛卡尔积原理)

--student表全部内容和mark表全部内容生成笛卡儿积select *from student,mark;--student表id,name和mark表grade生成笛卡儿积select student.id ,studen.name ,mark.grade from student ,mark;

内连接:用的最多

--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

insert student.id ,student.name,mark.grade from student left join mark on studen.id=mark.stu_id;

右外连接: 优先显示右表全部记录 ,保留右边的记录,相比内连接可以匹配左边的null

insert student.id ,student.name,mark.grade from student right join mark on studen.id=mark.stu_id;

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

//将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. 嵌套查询(子查询)

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

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。