SQL的初步学习(二)(以MySQL为例)

一、SQL 基础语法

3.DQL(数据查询语言)

常用比较运算符

常用逻辑运算符

主要语法

sql 复制代码
select 字段列表 from 表名 where 条件列表;

单表查询

(1)基本查询
查询多个字段:
sql 复制代码
select 字段1,字段2,字段3... from 表名;
查询所有字段(通配符):
sql 复制代码
select * from 表名;
设置别名:

as关键字可以省略,若别名中有空格等特殊字符需用单引号包裹别名。

sql 复制代码
select 字段1 [as 别名1],字段2 [as 别名2] from 表名;

未设置别名前

设置别名后

去除重复记录:

distinct可以放在需要去除重复的字段前,修饰这个字段。

sql 复制代码
select distinct 字段列表 from 表名;
练习题:

1757. 可回收且低脂的产品https://leetcode.cn/problems/recyclable-and-low-fat-products/

sql 复制代码
select product_id 
from Products 
where low_fats = 'Y' and recyclable = 'Y';

584. 寻找用户推荐人https://leetcode.cn/problems/find-customer-referee/

sql 复制代码
select name
from Customer
where referee_id != 2 or referee_id is null;

595. 大的国家https://leetcode.cn/problems/big-countries/

sql 复制代码
SELECT name,population,area 
FROM World
WHERE area>=3000000 OR population >=25000000;
(2)分组查询
基础语法
sql 复制代码
select 聚合函数(字段列表) from 表名;
常用的聚合函数
注意
练习题:

1211. 查询结果的质量和占比https://leetcode.cn/problems/queries-quality-and-percentage/

sql 复制代码
SELECT query_name,
ROUND(AVG(rating*1.0/position),2) AS quality,-- round函数为四舍五入函数
ROUND(AVG(IF(rating<3,1.0,0.0))*100,2) AS poor_query_percentage-- if函数后续会讲
FROM Queries
GROUP BY query_name;
(3)分组查询
基础语法

group by后可以接多个字段,分组时依次按字段分组。

sql 复制代码
select 字段列表 from 表名 [where 条件] group by 分组字段名 [having 分组后过滤条件];
where与having区别

1.执行时机不同:where分组之前进行过滤,不满足where条件,不参与分组;而having分组之后对结果进行过滤。

2.判断条件不同:where不能 对聚合函数进行判断,而having****可以

注意
练习题:

2356. 每位教师所教授的科目种类的数量https://leetcode.cn/problems/number-of-unique-subjects-taught-by-each-teacher/

sql 复制代码
select teacher_id,count(distinct subject_id) as cnt
from Teacher
group by teacher_id;
(4)排序查询
基础语法

order by后可以接多个字段,排序时当前面的字段全部相等时按该字段排序。不写排序方式默认是升序排序。

sql 复制代码
select 字段列表 from [where 条件列表][group by 分组字段] order by 字段1 排序方式1,字段2 排序方式2...;
排序方式
练习题:

620. 有趣的电影https://leetcode.cn/problems/not-boring-movies/

sql 复制代码
SELECT id,movie,description,rating
FROM cinema
WHERE id%2=1 AND description!='boring'
ORDER BY rating DESC;-- 降序排序
(5)分页查询
基础语法
sql 复制代码
select 字段列表 from 表名 limit 起始索引,查询记录数;
注意

多表查询

(1)连接查询
a.内连接

相当于查询两表交集的部分数据。

基础语法

隐式内连接

sql 复制代码
select 字段列表 from 表1,表2 where 条件...;

显式内连接

inner可以省略不写。

sql 复制代码
select 字段列表 from 表1 [inner] join 表2 on 连接条件...;

练习题:

1068. 产品销售分析 Ihttps://leetcode.cn/problems/product-sales-analysis-i/

sql 复制代码
select p.product_name,s.year,s.price
from Sales s,Product p  -- 给表名取别名,只是方便写而已
where s.product_id=p.product_id;

或者

sql 复制代码
select p.product_name,s.year,s.price
from Sales s inner join Product p on  -- 给表名取别名,只是方便写而已
s.product_id=p.product_id;
b.外连接

左连接时以左边的表为基础,去连接右边的表;而右连接恰好相反。

outer可以省略不写。

左外连接

sql 复制代码
select 字段列表 from 表1 left [outer] join 表2 on 连接条件...;

右外连接

sql 复制代码
select 字段列表 from 表1 left [outer] join 表2 on 连接条件...;

练习题

1378. 使用唯一标识码替换员工IDhttps://leetcode.cn/problems/replace-employee-id-with-the-unique-identifier/

sql 复制代码
select d.unique_id,e.name 
from Employees e left join EmployeeUNI d 
on e.id=d.id;
(2)子查询
相关推荐
Database_Cool_9 小时前
OLTP 和 OLAP 区别详解:分析型数据库和事务型数据库怎么选(附阿里云 AnalyticDB MySQL 选型指南)
数据库·mysql·阿里云
Database_Cool_10 小时前
单机 MySQL 迁移到分布式数据库方便吗?阿里云 PolarDB-X 100% MySQL 协议兼容零改造平滑迁移
数据库·分布式·mysql
姜太小白10 小时前
【MySQL】 索引优化实战:解决 WHERE 等值 + IS NULL 查询,TEXT 字段报错 1167 的完整指南
数据库·mysql
a11177611 小时前
2FA 验证码生成器(github登录验证 app)
笔记·学习
我的xiaodoujiao11 小时前
API 接口自动化测试详细图文教程学习系列32--Allure测试报告2
python·学习·测试工具·pytest
liuxiaowei312 小时前
【绝版教程】新版MySQL DBA高级实战进阶班 MySQL8.0 姜承尧-腾讯数据库总监
数据库·mysql·dba
破碎的南瓜12 小时前
sqli--sql注入过关笔记(1,2,3,4,5,9)
数据库·笔记·sql
你想知道什么?13 小时前
线性回归-学习笔记
笔记·学习·线性回归
鱼毓屿御13 小时前
从「只会聊」到「边想边做」的跃迁
前端·学习·react.js
Csvn13 小时前
📊 SQL 入门 Day 10:递归 CTE — 破解无限层级查询的终极武器
后端·sql