惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

The GitHub Blog
The GitHub Blog
I
InfoQ
U
Unit 42
WordPress大学
WordPress大学
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Apple Machine Learning Research
Apple Machine Learning Research
J
Java Code Geeks
月光博客
月光博客
D
Docker
Stack Overflow Blog
Stack Overflow Blog
D
DataBreaches.Net
阮一峰的网络日志
阮一峰的网络日志
Blog — PlanetScale
Blog — PlanetScale
V
Visual Studio Blog
博客园 - 聂微东
A
About on SuperTechFans
腾讯CDC
Jina AI
Jina AI
Microsoft Azure Blog
Microsoft Azure Blog
GbyAI
GbyAI
博客园 - 【当耐特】
罗磊的独立博客
博客园 - 三生石上(FineUI控件)
M
MIT News - Artificial intelligence

Shiroha白羽的博客

Golang 踩坑 —— interface 为参数的时候传 nil 指针 Codeforces Round 925 (Div. 3) Codeforces Round 924 (Div. 2) Codeforces Round 923 (Div. 3) Codeforces Round 922 (Div. 2) Codeforces Round 921 (Div. 2) Educational Codeforces Round 161 (Rated for Div. 2) Codeforces Round 920 (Div. 3) Codeforces Round 919 (Div. 2) Hello 2024 Good Bye 2023 Codeforces Round 918 (Div. 4) 个人备份的常用 macOS 清理命令 Codeforces Round 917 (Div. 2) Pinely Round 3 (Div. 1 + Div. 2) Educational Codeforces Round 160 (Rated for Div. 2) Codeforces Round 915 (Div. 2) Codeforces Round 914 (Div. 2) Codeforces Round 913 (Div. 3) Educational Codeforces Round 159 (Rated for Div. 2) Codeforces Round 912 (Div. 2) Codeforces Round 911 (Div. 2) CodeTON Round 7 (Div. 1 + Div. 2, Rated, Prizes!) Educational Codeforces Round 158 (Rated for Div. 2) Codeforces Round 910 (Div. 2) Codeforces Round 909 (Div. 3) Codeforces Round 908 (Div. 2) Educational Codeforces Round 157 (Rated for Div. 2) C++自定义的字面量 Codeforces Round 907 (Div. 2)
记一次 SQL LEFT JOIN 没有得到预期结果的错误
Shiroha · 2022-05-29 · via Shiroha白羽的博客

最近在业务中做数据开发的时候,写了一个 SQL 但是没有得到预期的结果,大致如下

1
2
3
4
5
6
7
8
9
10
表 a
+----+------+-----+
| id | name | tid |
+----+------+-----+
| 1 | aaa | 101 |
+----+------+-----+
| 2 | bbb | 102 |
+----+------+-----+
| 3 | ccc | 103 |
+----+------+-----+
1
2
3
4
5
6
7
8
9
10
表 b
+------+------+-------+
| id | nick | type |
+------+------+-------+
| 1001 | abc | false |
+------+------+-------+
| 1002 | edf | true |
+------+------+-------+
| 1003 | xyz | true |
+------+------+-------+

然后圈选的 SQL 的为

1
2
3
4
5
6
7
8
9
10
11
12
SELECT
a.name
b.nick
FROM
a
LEFT JOIN
b
ON
a.tid = b.id
WHERE
b.type = "true"
;

本意上,通过 LEFT JOIN ,即使没有找到,也应该正常返回数据,但是实际上没有返回任何数据

因为 WHERE 条件是在 JOIN 之后发生的,所以实际上,因为 LEFT JOIN 拿不到数据,所以所有列的 b.type 都是 NULL,当然就不是 true

此时可以拆分这两个条件,例如

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
@true_b :=
SELECT
id,
nick
FROM
b
WHERE
type = "true"
;

SELCT
a.name
c.nick
FROM
a
LEFT JOIN
@true_b c
ON
a.tid = c.id