MySQL常用语句(CURD)

文章目录

  • 一、基础操作
    • 1-1 库表操作
    • 1-2 insert
    • 1-3 select
      • ①列查询
      • ②查询字段为表达式
      • ③去重:distinct
      • ④排序:order by
      • ⑤条件查询:where
      • ⑥分页查询:limit
    • 1-4 update
    • 1-5 delete
  • 二、高阶操作
    • 2-1 复制
    • 2-2 聚合查询
      • ①聚合函数
      • ②group by子句
      • ③having
    • 2-3 联表查询
      • 2-3-1 内连接
      • 2-3-2 外连接
      • 2-3-3 自连接
      • 2-3-4 子查询
      • 2-3-5 合并查询


提示:以下是本篇文章正文内容,Java系列学习将会持续更新

一、基础操作

1-1 库表操作

库的相关操作:

-- 建库
create database test_db;
create database java_0326 default charset utf8mb4;

-- 设置默认库,表示之后的操作都是对该库进行的
use test_db;

-- 展示所有库
show databases;
-- 展示默认库
select database();

-- 删库
drop datebase if exists java_0326;

表的相关操作:

-- 建表
CREATE TABLE 表名称 (
    列名称   类型        约束信息,
	id       int         not null,
	name     varchar(30), 
	age      int
);
-- 例如
create table persons (id int primary key);

-- 展示默认库的所有表名称
show tables;

-- 删表
drop table if exists student;
-- 清空表数据(截断)
truncate table student;

回到目录…

1-2 insert

①单行+全列插入

insert into student values (100, 10000, ‘唐三藏’, NULL);
insert into student values (101, 10001, ‘孙悟空’, 12345);

②多行+指定列插入

insert into student (id, sn, name) values
	(102, 20001, ‘刘大耳’),
	(103, 20002, ‘曹阿瞒’),
	(104, 20003, ‘孙仲谋’);

回到目录…

1-3 select

①列查询

-- 全列查询
-- 不建议使用,因为数据过大的话,容易使系统崩掉
select * from student;

-- 指定列查询
-- 指定的列可以不按定义表的顺序来
select id, name, english from student;

②查询字段为表达式

-- 表达式不包含字段
select id, name, 10 from student;
-- 表达式包含字段
select id, name, (chinese + math + english) / 3 from student;
-- 结果集中,表头列名为别名
select id, name, chinese + math + english 总分 from student;
-- 结果集分三列,id、math、0/1
select id, math, math > 60 from student;
-- 有null参与的运算结果还是null
-- 即使是null,作bool值的时候,也看作false
select qq_mail = null from student;

③去重:distinct

-- 可以列出所有不重复的math成绩集合
select distinct math from student;
-- 此时就没有去重功能了,因为没有id和math同时重复的学生
select distinct id, math from student;

④排序:order by

-- asc : ascend 升序 不写默认为升序
-- desc : descend 降序
-- NULL视为比任何数据都小
-- 先优先math升序;若math相等,则按Chinese降序;若都相等,按id升序
select * from student order by math asc, chinese desc, id;
-- 使用表达式 + 别名排序
select  name, math + chinese + english 总分 from student order by 总分;

回到目录…

⑤条件查询:where

比较运算符 说明
>, >=, <, <= 和Java中一样
= 等于,NULL不安全,例如 NULL = NULL 的结果是NULL
<=> 等于,NULL安全,例如NULL <=> NULL 的结果是TRUE
!=, <> 不等于
between a0 and a1 范围匹配[a0, a1],如果a0<= value<= a1,返回TRUE
in (option, …) 如果是 option 中的任意一个,返回 TRUE
is null 是 NULL
Is not null 不是 NULL
like 模糊匹配,如 like ‘孙%’ 或 like ‘孙_’
逻辑运算符 说明
and 条件都满足,取true
or 满足其中一个条件,取true
not 条件为true,结果取false
-- 基本查询
-- 查询english成绩高于60分的学生
select name, english from student where english > 60;
-- 查询总分高于240分的学生
select  name, math + chinese + english 总分 from student 
		where math + chinese + english > 240;

-- and与or
select * from student where english > 60 and math > 60;
select * from student where english > 90 or math > 90;

-- 范围查询
select * from student where english between 80 and 100;

-- in查询
-- 查询math成绩是这五个数据之一的学生
select * from student where math in (80, 85, 90, 95, 100);

-- 模糊查询
-- %匹配任意多个(包括0个)字符
select name from student where name like ‘孙%;
select name from student where name like%孙’;
select name from student where name like%%;
-- _严格匹配一个任意字符
select name from student where name like ‘孙_’;

-- NULL查询
-- 查询qq_mail已知的同学
select name, qq_mail from student where qq_mail is not null;
-- 查询qq_mail未知的同学
select name from student where qq_mail is null;

回到目录…

⑥分页查询:limit

-- 从下标0开始,筛选5条结果
select * from student limit 5;
-- 从下标2开始,筛选5条结果
select * from student limit 2, 5;
-- 筛选5条结果,从下标2开始。这种写法意思更明确
select * from student limit 5 offset 2;

-- 综合运用
-- 查询数学成绩高于80分,且排名前三的学生
select name, math from student where math > 80 order by math desc limit 3;

回到目录…

1-4 update

-- 修改孙悟空的数学成绩为80分
update student set math = 80 where name = ‘孙悟空’;
-- 修改曹孟德的数学成绩为80分,语文成绩为90分
update student set math = 80, chinese = 90 where name = ‘曹孟德’;

-- 将总分倒数三名同学的数学成绩加上20分  (支持limit,不支持offset)
update student set math = math + 80 order by Chinese + math + english limit 3;

-- 将所有同学的语文成绩更新为2倍
update student set chinese = chinese * 2;

回到目录…

1-5 delete

-- 删除孙悟空的考试成绩
delete from student where name = ‘孙悟空’;

-- 将数据表中的某个字段从表中删除
alter table 表名 drop 字段名;

-- 删除整张表及数据
drop table if exists student;

-- 仅删除表的全部数据  for-each
delete from student;
-- 清空:表截断 O(1)
truncate table student;

回到目录…

二、高阶操作

2-1 复制

--将学生表的数据复制到用户表
insert into user (name, email) select name, qq_email from student;

2-2 聚合查询

①聚合函数

-- 聚合函数是将多行数据合并为一行返回结果
-- count
-- 统计数据的数量
select count(*) from student;
select count(0) from student;
-- 统计邮箱的个数,qq_mail为null不计入结果
select count(qq_mail) from student;

-- sum
-- 统计所有学生的数学总分
select sum(math) from student;

-- avg
-- 统计班级数学平均分
select avg(math) from student;

-- max
-- 统计班级数学最高分
select max(math) from student;

-- min
-- 统计班级数学最低分
select min(math) from student;

student表:
MySQL常用语句(CURD)_第1张图片

②group by子句

-- group by子句可以对指定列进行分组查询
-- 按照规范,select查询到的内容应该是聚合的内容
-- 查询每种角色的最高工资、最低工资、平均工资
select role, max(salary), min(salary), avg(salary) from emp group by role;

-- 可以先where筛选后再聚合
-- 统计每种角色薪资高于500的人数
select role, count(*) from emp where salary > 500 group by role;

-- 多聚合,在不同列的组合下进行聚合查询
select company, count(*) from emp2 group by company;
select company, depart, count(*) from emp2 group by company, depart;
select company, depart, role, count(*) from emp2 group by company,depart,role;

emp2表:
MySQL常用语句(CURD)_第2张图片

③having

-- having关键字用于聚合之后进行过滤操作
select role, avg(salary) from emp group by role having avg(salary) > 300;

回到目录…

2-3 联表查询

实际开发中往往数据来自不同的表,所以需要多表联合查询。多表查询是对多张表的数据取笛卡尔积。

两张表:
MySQL常用语句(CURD)_第3张图片

2-3-1 内连接

select 字段 from 表1 join 表2 on 连接条件 where 其他条件;

-- 添加 联表 条件后,得到的结果才是有意义的
-- 标准写法
select * from users join articles on uid = author_id where users.name = '小红';

-- 也可以这么写
select * from users, articles where uid = author_id and users.name = '小红';

-- inner 可以省略
select * from users inner join articles on uid = author_id;	

内连查询结果:
MySQL常用语句(CURD)_第4张图片

回到目录…

2-3-2 外连接

① 左外连接,表1完全显示
select 字段名 from 表名1 left join 表名2 on 连接条件;
② 右外连接,表2完全显示
select 字段 from 表名1 right join 表名2 on 连接条件;

-- 左外联
select * from users left outer join articles on uid = author_id; 
select * from users left join articles on uid = author_id; 

左外连查询结果:
MySQL常用语句(CURD)_第5张图片

-- 右外联
select * from users right outer join articles on uid = author_id;
select * from users right join articles on uid = author_id;

右外连查询结果:
MySQL常用语句(CURD)_第6张图片

回到目录…

2-3-3 自连接

自连接是指在同一张表连接自身进行查询。

原表: course表、score表
MySQL常用语句(CURD)_第7张图片
显示所有 “Java” 成绩比 “计算机原理” 成绩低的成绩信息

SELECT s1.student_id, s1.score, s2.score 
FROM score s1
JOIN score s2 
ON s1.student_id = s2.student_id  -- 指向同一个学生
AND s1.course_id = 1  -- 表1指向Java成绩
AND s2.course_id = 3  -- 表2指向计算机原理成绩
AND s1.score < s2.score; -- Java < 计算机原理

查询结果:
在这里插入图片描述

回到目录…

2-3-4 子查询

单行子查询:返回一行记录的子查询。

案例:查询与“不想毕业” 同学的同班同学:

select * from student where classes_id=(select classes_id from student where name='不想毕业');

多行子查询:返回多行记录的子查询。

案例:查询“语文”或“英文”课程的成绩信息

-- 使用IN
select * from score where course_id in (select id from course where name='语文' or name='英文');

-- 使用 NOT IN
select * from score where course_id not in (select id from course where name!='语文' and name!='英文');

-- 使用 EXISTS
select * from score sco where exists (select sco.id from course cou where (name='语文' or name='英文') and cou.id = sco.course_id);

-- 使用 NOT EXISTS
select * from score sco where not exists (select sco.id from course cou where (name!='语文' and name!='英文') and cou.id = sco.course_id);

回到目录…

2-3-5 合并查询

在实际应用中,为了合并多个select的执行结果,可以使用 unionunion all 时,前后查询的结果集中,字段需要一致。

union : 该操作符用于取得两个结果集的并集。当使用该操作符时,会自动去掉结果集中的重复行。 案例:查询id小于3,或者名字为“英文”的课程.

select * from course where id<3 union
select * from course where name='英文';

-- 或者使用or来实现
select * from course where id<3 or name='英文';

union all : 该操作符用于取得两个结果集的并集。当使用该操作符时,不会去掉结果集中的重复行。
案例:查询id小于3,或者名字为“Java”的课程.

-- 可以看到结果集中出现重复数据Java select * from course where id<3 union all
select * from course where name='英文';

回到目录…


总结:
提示:这里对文章进行总结:
以上就是今天的学习内容,本文是MySQL的学习,我们学习整理了MySQL常用语句:基础的CURD操作和高阶的聚合查询和联表查询。之后的学习内容将持续更新!!!

你可能感兴趣的:(Linux,+,MySQL,mysql,数据库,sql)