【MySQL的DQL查询语句】

MySQL的DQL查询语句-----在Navicat下

  • 将学生表导入Navicat中
  • 查询语句
  • [合并语句(left join right join 等等)](#合并语句(left join right join 等等))
    • [inner join 内连接(默认的连接方式)](#inner join 内连接(默认的连接方式))
    • [left join (左连接)](#left join (左连接))
    • [right join(右连接)](#right join(右连接))
    • [union (上下合并)并且去重](#union (上下合并)并且去重)
    • [union all (上下合并)并且不去重](#union all (上下合并)并且不去重)
    • 全连接
    • 笛卡尔积
  • [where in 使用方法](#where in 使用方法)
    • [自增列插入数据 自增列数据使用null 或 0 占位](#自增列插入数据 自增列数据使用null 或 0 占位)
  • 复习
    • [updata 语句 更新语句 delete 语句 删除语句](#updata 语句 更新语句 delete 语句 删除语句)
    • [主键 ,FOREIGN KEY 约束建](#主键 ,FOREIGN KEY 约束建)
  • 全部代码

在上个DDL博客中找到学生表将其中的数据读入到mysql中,使用虚拟机或者Navicat都可以。

将学生表导入Navicat中


查询语句

查询一整张表

select * from students;

查询年龄大于22

select *

from students

where students.age > 22;

年龄大于22的女生

select clazz

,sname

,age

,gender

from students

where students.age > 22 and students.gender = "女";

查找文科的学生

select *

from students

where clazz like "%文科%"; # %为模糊匹配

查找六班的学生

select *

from students

where clazz like "%六班%";

select *

from students

where clazz like "%六%";

计算学生的总分 (group by)

group by 后面的字段必须在select 里面体现出
并且不能体现出不在group by 之后的字段 只有
sum avg count max min 这些函数才可以另外体现

select score.id

,sum(sco)

from score

group by score.id;

这是计算全部学生的总和 可以看成是一个值 在where 判断语句中可以直接用

select sum(sco) from score;

合并两表 (join on xxxx)

select t1.*

,t2.sub_id

,t2.sco

from students as t1

join score as t2

on t1.id = t2.id;

合并两张表 并求总分

先合并在聚合

select tt1.id

,tt1.sname

,tt1.gender

,tt1.clazz

,sum(tt1.sco) as sum_sco

,max(tt1.sco) as max_sco

from (select t1.*

,t2.sco

from students as t1

join score as t2

on t1.id = t2.id ) as tt1

group by tt1.id,tt1.sname,tt1.gender,tt1.clazz;

先聚合在合并

select t2.*

,t1.sum_sco

from (select score.id

,sum(score.sco) as sum_sco

from score

group by score.id) as t1

join students as t2

on t1.id = t2.id;

找到总分分数大于500的学生

having方法(在group by 之后执行)

select tt1.id

,tt1.sname

,tt1.age

,tt1.gender

,tt1.clazz

,sum(sco) as sum_sco

from (select t1.*

,t2.sub_id

,t2.sco

from students as t1

join score as t2

on t1.id = t2.id ) as tt1

group by tt1.id,tt1.sname,tt1.age,tt1.gender,tt1.clazz

having sum_sco > 500;

用where 方法来选择

select t1.*

,t2.sum_sco

from students as t1

join (select score.id

,sum(sco) as sum_sco

from score

group by score.id) t2

on t1.id = t2.id

where t2.sum_sco > 500;

降序排列 (order by xxxx desc )

select tt1.id

,tt1.sname

,tt1.age

,tt1.gender

,tt1.clazz

,sum(sco) as sum_sco

from (select t1.*

,t2.sub_id

,t2.sco

from students as t1

join score as t2

on t1.id = t2.id ) as tt1

group by tt1.id,tt1.sname,tt1.age,tt1.gender,tt1.clazz

having sum_sco > 500

order by sum_sco desc # 不加desc 为升序排列

只展示5个学生 (limit number)

select tt1.id

,tt1.sname

,tt1.age

,tt1.gender

,tt1.clazz

,sum(sco) as sum_sco

from (select t1.*

,t2.sub_id

,t2.sco

from students as t1

join score as t2

on t1.id = t2.id ) as tt1

group by tt1.id,tt1.sname,tt1.age,tt1.gender,tt1.clazz

having sum_sco > 500

order by sum_sco desc # 不加desc 为升序排列

limit 5;

求前三门课程总分(where 在group by 之前执行)

select score.id

,sum(sco) as sum_sco

from score

where score.sub_id = 1000001

or score.sub_id = 1000002

or score.sub_id = 1000003

group by score.id;

合并语句(left join right join 等等)

设置两个表a和b其中的数据如下

a表 (id 学号)(name 姓名)

b表 (id 学号) (s_id 学科编号)

inner join 内连接(默认的连接方式)

只会合并两者都有的元素 没有的元素直接舍弃

left join (左连接)

以左边为基准进行左连接 没有的值则为null显示

right join(右连接)

以右边的表为基准进行合并 没有的值则补位空值

union (上下合并)并且去重

union all (上下合并)并且不去重

全连接

sql中实际上不提供全连接 但是如果把左连接根右连接合并则就是全连接

笛卡尔积

每个a数据都要根b数据合并一次

where in 使用方法


自增列插入数据 自增列数据使用null 或 0 占位

insert into users() values(null,"zp");

insert into users() values(0,"yn");

复习

order 的执行顺序还在select 之后

大致的执行顺序为

== from > where > group by > select > having > order by > limit==

括号里面的优先级最高

sql 复制代码
-- select 语句
select * 
from table
where conditions
group by columns
having conditions
order by columns
limit start,length;
sql 复制代码
join (left right inner) 三种形式
union (去重) union all 去重和不去重的区别

updata 语句 更新语句 delete 语句 删除语句

sql 复制代码
-- updata 语句 更新语句
UPDATE students set gender='1' where gender='男';
UPDATE students set gender='0' where gender='女';
UPDATE students set gender='1';

-- delete 语句 删除语句
delete from students where gender='女';
delete from students;
-- 截断表 将表删除并清空 所有的东西重新开始刷新 在进行新的书写
truncate students;

主键 ,FOREIGN KEY 约束建

主键是唯一的 是唯一约束 并且特性是 非空且唯一

FOREIGN KEY 这个是与另外一个表关联 ,当另外一个表变化后 子表也会变化

全部代码

sql 复制代码
-- 查询一整张表
select * from students;

-- 查询年龄大于22
select *
from students
where students.age > 22;

-- 年龄大于22的女生
select clazz
			 ,sname
			 ,age
			 ,gender
from students
where students.age > 22 and students.gender = "女";

-- 查找文科的学生
select *
from students
where clazz like "%文科%"; # %为模糊匹配


-- 查找六班的学生
select *
from students
where clazz like "%六班%";

select *
from students
where clazz like "%六%";

-- 计算学生的总分  
# group by 后面的字段必须在select 里面体现出
# 并且不能体现出不在group by 之后的字段 只有 
# sum avg count max min 这些函数才可以另外体现
select score.id
       ,sum(sco)
from score
group by score.id;

# 这是计算全部学生的总和 可以看成是一个值 在where 判断语句中可以直接用
select sum(sco) from score;

-- 合并两表
select t1.*
       ,t2.sub_id
			 ,t2.sco
from students as t1
join score as t2
on t1.id = t2.id;

-- 合并两张表 并求总分

# 先合并在聚合 
select tt1.id
       ,tt1.sname
			 ,tt1.gender
			 ,tt1.clazz
			 ,sum(tt1.sco) as sum_sco
			 ,max(tt1.sco) as max_sco
from (select t1.*
			       ,t2.sco
from students as t1
join score as t2
on t1.id = t2.id ) as tt1
group by tt1.id,tt1.sname,tt1.gender,tt1.clazz;


# 先聚合在合并
select t2.*
			 ,t1.sum_sco
from (select score.id
             ,sum(score.sco) as sum_sco
from score 
group by score.id) as t1
join students as t2
on t1.id = t2.id;

-- 找到总分分数大于500的学生
# having 在group by 之后执行
select tt1.id
			 ,tt1.sname
			 ,tt1.age
			 ,tt1.gender
			 ,tt1.clazz
			 ,sum(sco) as sum_sco
from (select t1.*
       ,t2.sub_id
			 ,t2.sco
from students as t1
join score as t2
on t1.id = t2.id ) as tt1
group by tt1.id,tt1.sname,tt1.age,tt1.gender,tt1.clazz
having sum_sco > 500;

# where 方法 !! where要在having前面执行
select t1.*
       ,t2.sum_sco
from students as t1
join (select score.id
       ,sum(sco) as sum_sco
from score 
group by score.id) t2
on t1.id = t2.id
where t2.sum_sco > 500;

-- 降序排列
select tt1.id
			 ,tt1.sname
			 ,tt1.age
			 ,tt1.gender
			 ,tt1.clazz
			 ,sum(sco) as sum_sco
from (select t1.*
       ,t2.sub_id
			 ,t2.sco
from students as t1
join score as t2
on t1.id = t2.id ) as tt1
group by tt1.id,tt1.sname,tt1.age,tt1.gender,tt1.clazz
having sum_sco > 500
## 降序排列
order by sum_sco desc # 不加desc 为升序排列
## 只展示5个学生
limit 5;

-- where 在group by 之前执行
-- 求前三门课程总分
select  score.id
        ,sum(sco) as sum_sco
from score
where score.sub_id = 1000001 
      or score.sub_id = 1000002 
			or score.sub_id = 1000003
group by score.id;
相关推荐
Java探秘者3 小时前
Maven下载、安装与环境配置详解:从零开始搭建高效Java开发环境
java·开发语言·数据库·spring boot·spring cloud·maven·idea
2301_786964363 小时前
3、练习常用的HBase Shell命令+HBase 常用的Java API 及应用实例
java·大数据·数据库·分布式·hbase
苹果醋33 小时前
大模型实战--FastChat一行代码实现部署和各个组件详解
java·运维·spring boot·mysql·nginx
阿维的博客日记4 小时前
图文并茂解释水平分表,垂直分表,水平分库,垂直分库
数据库·分库分表
wrx繁星点点5 小时前
事务的四大特性(ACID)
java·开发语言·数据库
小小娥子6 小时前
Redis的基础认识与在ubuntu上的安装教程
java·数据库·redis·缓存
DieSnowK6 小时前
[Redis][集群][下]详细讲解
数据库·redis·分布式·缓存·集群·高可用·新手向
计算机学姐6 小时前
基于SpringBoot+Vue的高校运动会管理系统
java·vue.js·spring boot·后端·mysql·intellij-idea·mybatis
-XWB-6 小时前
【MySQL】数据目录迁移
数据库·mysql
老华带你飞6 小时前
公寓管理系统|SprinBoot+vue夕阳红公寓管理系统(源码+数据库+文档)
java·前端·javascript·数据库·vue.js·spring boot·课程设计