MySQL 基础查询 ,连接查询,子查询

MySQL查询

级联:
on delete casscade 删除级联
on update csacde 更新级联
删除数据时:需要先删除外键表,在删除主键表
录入数据时:先录入主键,再录入外键

当删除主表数据主表数据变化从表跟着变化,修改主表数据变化修改从表跟着修改

查询:

select 列名1,列名2 
from 表名
where 条件表达式
group by 列名
having 条件表达式
order by 列名
limit ?,? 分页

查询表时可以指定查询某些列,顺序可以自定义(查询全部列用‘select *’)
例如:select name,age,address,sex from student;
列和表都可以起别名,查询中列支持运算

去重查询(尽量少用,影响性能)

select distinct 列名 from 表名

条件查询:
条件运算符:= > < >= <= 不等:<>和!=
算术运算符:+ - * / 不支持:++ – 符合赋值 += -=
逻辑运算符:
and(&&):并且:多个条件同时成立就true 否则返回false
not (!):非:取反
or(||):或,多个条件只有一个成立,结果true,都不成立false

范围查询:
and运算符连接条件:between a and b ;(根据a,b范围来查询数据)

select * from studentinfo where age between 19 and 21; 

in(在…内);匹配某个字段的值是几个可能的选项值之一,不是范围是确定值

select * from student where age in(15,17,20); 

模糊查询

通配符:
“.”匹配任意字符
“[ ]”匹配在[ ] 内的任意一个字符
“ [^]”匹配不在[ ] 内的任意一个字符
“*” 匹配零个或多个在它前面的字符
“+”匹配+前面的字符一次或多次
“{n}”匹配前面的字符至少n次
“^” 匹配文本的开始字符
“$”匹配文字结尾字符

例如://匹配名字以a开头 c结尾的名字

select * from student where name regexp '^a.c$';

模糊查询like关键字
使用通配符
‘ %a ’ 以a为结尾的数据
‘ a% ’ 以a开头的数据
‘ %a% ’ 含有a是数据,三位且中间是a的
‘_ a ’ 两位且结尾是a的
‘ a_ ’ 两位且开头是a的

%:表示任意0个或者过个字符,可匹配任意类型长度的字符,有情况下若是中文,请使用两个百分号(%%)表示
_ :表示任意单个字符。匹配单个任意字符,常用来限制表达式的字符长度的语句

例如:
– 查询张姓同学名字是两个字的

select * from student where `name` like '张_'; 

– 查询名字中带有王字的

select * from student where `name` like '%王%';

– 查询名字中间是中字的

select * from student where `name` like '_中_'; 

– 姓李的 地址里有周口的

select * from student where `name` like '李%' and address like '%周口%'; 

排序操作

order by 列名 asc 升序(默认)desc 降序

聚合函数

count(*)列名为空不计入总数
max(列名) 取列中最大值
min (列名)取列中最小值
sum(列名) 求和
avg(列名)取平均值

分组查询

group by 列名 是将某一列相同数据行当成一组

having :对分组后的数据过滤。 group by 列名 having 条件

分页查询 limit
防止数据过多产生爆炸,可以查询一部分数据
用法:select * from student limit(3,5);(索引 , 查询的个数)

常用查询语法:

select 列名 from 表名
where 条件 
group by 分组列名 
having 过滤条件 
order by 排序列名
limit start ,length;

where 等值连接:必须加条件,查询结果才有意义

select * from student,classInfo
where student.classId = classinfo.classId

inner join 内连接(等值连接)

在这里插入图片描述

select * from1 别名1
[inner] join2 别名2
on 别名1.对等数据列 = 别名2.对等数据列 # 必须加条件
where 条件
group byhaving 条件
order bylimit 位置,记录数
limit 位置,记录数

左外连接:left outer join
以左表为主,左表数据全部出来,右表匹配上的数据显示,匹配不上的不显示
在这里插入图片描述

select s*,c* 
from student s			 # 写在前面的表称为左表
left outer join classInfo c		 #写在后面的表称为右表
on s.classId = c.classId

在这里插入图片描述

#右外连接: right outer join
以右表为主,右表数据全部出来,左表匹配上的数据出来,匹配不上的不显示

在这里插入图片描述


select s.*,c.*
from student s # 写在前面的表称为左表
right outer join classInfo c #写在后面的表称为右表
on s.classId = c.classId

在这里插入图片描述

#前查询所有学生的:班级名字,学生姓名,性别,课程名字,分数并且按照每科目倒序排列

select
ci.className , s.name , s.sex , co.courseName , e.score
from student s , classInfo ci , course co , exam e
where s.classId = ci.classId
and s.studentId = e.studentId
and co.courseId = e.courseId
order by co.courseName asc,e.score desc

inner join连接

select ci.className,s.name,s.sex,co.courseName,e.score
from student s 
join classInfo ci
on s.classId = ci.classId # 两表连接后变成一张表
join exam e 
on s.studentId = e.studentId
join course co
on e.courseId = co.courseId

子查询
在查询中再嵌套另一个查询(套娃)

例如: 学生的班级编号等于A1的班级编号

select * from student
where cid = (
  select cid from classInfo where cname = 'A1' #主要用来获取符合条件的学生id然后用于查询student表
)

在这里插入图片描述
在这里插入图片描述

查询语文课程,并且分数大于78分的学生

select * from student 
where studentId in ( #根据学生id查询到学生信息
  select studentId from exam
  where courseId = ( #根据课程id查询到学生id
    select courseId from course where courseName = '语文' #获取课程id
  ) and score>78
)

多表关联:
小节:
多表之间有对等列才能连接
where等值连接:两表相互匹配的数据会显示,不匹配的不显示
inner join : 内连接(等值效果)
left outer join : 左外连接(以左表为主,左表数据全部出来)
right outer join:右外连接(右表为主,右表数据全部出来)
cross join:交叉连接,笛卡尔积,不需要条件的连接

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值