Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
檀越@新空间
Coding Tree
提交
e7758a0d
C
Coding Tree
项目概览
檀越@新空间
/
Coding Tree
通知
2
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
C
Coding Tree
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
“5a9f53be92026aa995f721dfd7db274e6735052f”上不存在“...git@gitcode.net:paddlepaddle/Paddle-Lite.git”
提交
e7758a0d
编写于
4月 05, 2022
作者:
彭世瑜
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix
上级
5c247c03
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
261 addition
and
1 deletion
+261
-1
blog/php-mysql/index.md
blog/php-mysql/index.md
+2
-0
blog/php-mysql/sql-subquery.md
blog/php-mysql/sql-subquery.md
+244
-0
doc/miniprogram.md
doc/miniprogram.md
+15
-1
未找到文件。
blog/php-mysql/index.md
浏览文件 @
e7758a0d
...
@@ -43,3 +43,5 @@
...
@@ -43,3 +43,5 @@
[
联合查询 union
](
blog/php-mysql/sql-union.md
)
[
联合查询 union
](
blog/php-mysql/sql-union.md
)
[
连接查询 join
](
blog/php-mysql/sql-join.md
)
[
连接查询 join
](
blog/php-mysql/sql-join.md
)
[
子查询 sub query
](
blog/php-mysql/sql-subquery.md
)
blog/php-mysql/sql-subquery.md
0 → 100644
浏览文件 @
e7758a0d
# 子查询 sub query
## 1、基本概念
### 1.1、子查询
嵌套查询下层的程序模块,当一个查询是另一个查询的条件时,称之为子查询
一条select语句中,嵌入了另一条select语句
### 1.2、主查询
主要的查询对象,第一条select语句,确定所获取的数据目标(数据源)
### 1.3、子查询和主查询的关系
1.
子查询是嵌入到主查询中的
2.
子查询辅助主查询,要么作为条件,要么作为数据源
3.
子查询可以独立存在,是一条完整的select语句
### 1.4、子查询的分类
1、按功能分
1.
标量子查询:子查询返回的结果是一个数据(一行一列)
2.
列子查询:返回一列(一列多行)
3.
行子查询:返回一行(一行多列)
4.
表子查询:返回多行多列
5.
exists子查询 返回1或者0(类似布尔操作)
2、按位置分
1.
where子查询
2.
from子查询
## 2、标量子查询
查询结果是一个数据(一行一列)
### 2.1、基本语法
```
sql
-- 子查询得到的结果只有一个值
select
*
from
数据源
where
条件判断
=/<>
(
select
字段名
form
数据源
where
条件判断
);
```
### 2.2、示例
```
sql
-- 知道学生的id,查询所在班级名字
-- 主查询:班级,子查询:班级id
mysql
>
select
*
from
my_student
;
+
----+--------+----------+------+--------+
|
id
|
name
|
class_id
|
age
|
gender
|
+
----+--------+----------+------+--------+
|
1
|
刘备
|
1
|
18
|
2
|
|
2
|
李四
|
1
|
19
|
1
|
|
3
|
王五
|
2
|
20
|
2
|
|
4
|
张飞
|
2
|
21
|
1
|
|
5
|
关羽
|
1
|
22
|
2
|
|
6
|
曹操
|
1
|
20
|
NULL
|
+
----+--------+----------+------+--------+
mysql
>
select
*
from
my_class
;
+
----+--------+
|
id
|
name
|
+
----+--------+
|
1
|
一班
|
|
3
|
三班
|
|
2
|
二班
|
+
----+--------+
select
class_id
from
my_student
where
id
=
1
;
+
----------+
|
class_id
|
+
----------+
|
1
|
+
----------+
select
*
from
my_class
where
id
=
(
select
class_id
from
my_student
where
id
=
1
);
+
----+--------+
|
id
|
name
|
+
----+--------+
|
1
|
一班
|
+
----+--------+
```
## 3、列子查询
列子查询得到的结果是一列数据,一列多行
### 3.1、基本语法
```
sql
主查询
where
条件
in
(
列子查询
)
```
### 3.2、示例
```
sql
-- 获取有学生的班级名字
-- 1、找到学生表中的所有班级id
-- 2、找出班级表中对应的名字
select
distinct
(
class_id
)
from
my_student
;
+
----------+
|
class_id
|
+
----------+
|
1
|
|
2
|
+
----------+
select
name
from
my_class
where
id
in
(
select
distinct
(
class_id
)
from
my_student
);
+
--------+
|
name
|
+
--------+
|
一班
|
|
二班
|
+
--------+
```
## 4、行子查询
行子查询返回的结果是一行多列
-
字段元素:一个字段对应的值
-
行元素:多个字段合起来作为一个元素参与运算
### 4.1、基本语法
```
sql
主查询
where
条件
[(
构造一个行元素
)]
=
(
行子查询
);
```
### 4.2、示例
```
sql
获取班级年龄最大,且班级号最大的学生
1
、求年龄最大
2
、求班级号最大
3
、求出学生
-- 错误示例
select
*
from
my_student
having
age
=
max
(
age
)
and
class_id
=
max
(
class_id
);
-- 1、having在group by之后,代表group by执行了一次,聚合函数使用
-- 2、group by 一旦执行,结果就是只返回一行记录,第一行
select
max
(
age
),
max
(
class_id
)
from
my_student
;
+
----------+---------------+
|
max
(
age
)
|
max
(
class_id
)
|
+
----------+---------------+
|
22
|
2
|
+
----------+---------------+
select
*
from
my_student
where
(
age
,
class_id
)
=
(
select
max
(
age
),
max
(
class_id
)
from
my_student
);
Empty
set
(
0
.
01
sec
)
select
*
from
my_student
where
(
age
,
class_id
)
=
(
select
max
(
age
),
min
(
class_id
)
from
my_student
);
+
----+--------+----------+------+--------+
|
id
|
name
|
class_id
|
age
|
gender
|
+
----+--------+----------+------+--------+
|
5
|
关羽
|
1
|
22
|
2
|
+
----+--------+----------+------+--------+
```
总结:
标量子查询、列子查询、行子查询都属于where子查询
## 5、表子查询
表子查询返回结果是多行多列,与行子查询相似
行子查询需要行元素,表子查询没有
-
行子查询用于where条件判断,属于where子查询
-
表子查询用于from数据源,属于from子查询
### 5.1、基本语法
```
sql
select
字段表
from
(
表子查询
)
as
别名
[
where
]
[
group
by
]
[
having
]
[
order
by
]
[
limit
]
```
### 5.2、示例
```
sql
获取每个班级年龄最大的学生
-- 错误示例
select
*
from
my_student
group
by
class_id
having
age
=
max
(
age
);
将每个班年龄最大的学生排在最前面
order
by
针对结果进行
group
by
保留每组第一条数据
--
select
*
from
(
select
*
from
my_student
order
by
age
desc
)
as
t
group
by
t
.
class_id
;
+
----+--------+----------+------+--------+
|
id
|
name
|
class_id
|
age
|
gender
|
+
----+--------+----------+------+--------+
|
1
|
刘备
|
1
|
18
|
2
|
|
3
|
王五
|
2
|
20
|
2
|
+
----+--------+----------+------+--------+
```
https://www.bilibili.com/video/BV1Vx411g7uJ?p=51&spm_id_from=pageDriver
doc/miniprogram.md
浏览文件 @
e7758a0d
...
@@ -20,4 +20,18 @@
...
@@ -20,4 +20,18 @@
[
weapp-qrcode
](
https://github.com/tomfriwel/weapp-qrcode
)
微信小程序生成二维码工具
[
weapp-qrcode
](
https://github.com/tomfriwel/weapp-qrcode
)
微信小程序生成二维码工具
[
echarts-map
](
https://github.com/mouday/echarts-map
)
包含 echarts 用到的 map 地图文件
[
echarts-map
](
https://github.com/mouday/echarts-map
)
包含 echarts 用到的 map 地图文件
\ No newline at end of file
[
uni-app
](
https://uniapp.dcloud.io/
)
是一个使用 Vue.js (opens new window)开发所有前端应用的框架,开发者编写一套代码,可发布到iOS、Android、Web(响应式)、以及各种小程序(微信/支付宝/百度/头条/飞书/QQ/快手/钉钉/淘宝)、快应用等多个平台。
[
uView UI
](
https://www.uviewui.com/
)
: 是全面兼容nvue的uni-app生态框架,全面的组件和便捷的工具会让您信手拈来,如鱼得水
[
小程序扩展组件库
](
https://developers.weixin.qq.com/miniprogram/dev/platform-capabilities/extended/component-plus/
)
扩展组件是对小程序内置组件能力的补充,包括一些常见的功能组件,持续补充中。
[
wxml-to-canvas
](
https://github.com/wechat-miniprogram/wxml-to-canvas
)
:小程序内通过静态模板和样式绘制 canvas ,导出图片,可用于生成分享图等场景
[
miniprogram-computed
](
https://github.com/wechat-miniprogram/computed
)
小程序自定义组件扩展 behavior,计算属性 computed 和监听器 watch 的实现。在 data 或者 properties 改变时,会重新计算 computed 字段并触发 watch 监听器。
[
mobx-miniprogram-bindings
](
https://github.com/wechat-miniprogram/mobx-miniprogram-bindings
)
小程序的 MobX 绑定辅助库
[
Day.js
](
https://day.js.org/zh-CN/
)
Moment.js 的 2kB 轻量化方案,拥有同样强大的 API
\ No newline at end of file
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录