带有一些空值逻辑的 PostgreSQL 内连接和外连接
PostgreSQL INNER JOIN AND OUTER JOIN WITH SOME NULL VALUES LOGIC
我有这两个表:
games_during_trial
+---------------------------------+
|account_id |game |
+---------------------------------+
| 1 | Minecraft |
| 1 | Overwatch |
| 2 | NULL |
| 3 | God of War |
| 4 | Minecraft |
| 4 | Elden Ring |
| 5 | Minecraft |
+---------------------------------+
games_after_trial
+---------------------------------+
|account_id |game |
+---------------------------------+
| 1 | Overwatch |
| 1 | Elden Ring |
| 2 | Horizon |
| 2 | Elden Ring |
| 3 | Overwatch |
| 3 | Minecraft |
| 4 | Minecraft |
| 4 | God of War |
| 4 | Overwatch |
| 5 | NULL |
+---------------------------------+
此处提供架构 http://sqlfiddle.com/#!15/b8209c/1
CREATE TABLE games_during_trial (account_id int,game varchar);
INSERT INTO games_during_trial (account_id,game)
VALUES ('1', 'Minecraft'), ('1', 'Overwatch'), ('2', NULL), ('3', 'God of War'), ('4', 'Minecraft'), ('4', 'Elden Ring'), ('5', 'Minecraft');
CREATE TABLE games_after_trial (account_id int, game varchar);
INSERT INTO games_after_trial (account_id,game)
VALUES ('1', 'Overwatch'), ('1', 'Elden Ring'), ('2', 'Horizon'), ('2', 'Elden Ring'), ('3', 'Overwatch'), ('3', 'Minecraft'), ('4', 'Minecraft'), ('4', 'God of War'), ('4', 'Overwatch'), ('5', NULL);
我如何在 Postgresql 中加入它们,以便当有完整的比赛(account_id 和游戏)时它像往常一样加入,但是,当其中一个表中缺少游戏时,它仍然加入但放 NULL?所需的输出应如下所示:
Desired JOIN
+---------------------------------------------------------+
|account_id |games_during_trial |games_after_trial |
+---------------------------------------------------------+
| 1 | Minecraft | NULL |
| 1 | Overwatch | Overwatch |
| 1 | NULL | Elden Ring |
| 2 | NULL | Horizon |
| 2 | NULL | Elden Ring |
| 3 | God of War | NULL |
| 3 | NULL | Overwatch |
| 3 | NULL | Minecraft |
| 4 | Minecraft | Minecraft |
| 4 | Elden Ring | NULL |
| 4 | NULL | God of War |
| 4 | NULL | Overwatch |
| 5 | Minecraft | NULL |
+---------------------------------------------------------+
请帮忙。
你想要一个完整的外部连接:
SELECT COALESCE(gd.account_id, ga.account_id) AS account_id,
gd.game AS games_during_trial,
ga.game AS games_after_trial
FROM games_during_trial gd
FULL OUTER JOIN games_after_trial ga
ON gd.account_id = ga.account_id AND
gd.game = ga.game
WHERE gd.game IS NOT NULL OR ga.game IS NOT NULL
ORDER BY 1;
我有这两个表:
games_during_trial
+---------------------------------+
|account_id |game |
+---------------------------------+
| 1 | Minecraft |
| 1 | Overwatch |
| 2 | NULL |
| 3 | God of War |
| 4 | Minecraft |
| 4 | Elden Ring |
| 5 | Minecraft |
+---------------------------------+
games_after_trial
+---------------------------------+
|account_id |game |
+---------------------------------+
| 1 | Overwatch |
| 1 | Elden Ring |
| 2 | Horizon |
| 2 | Elden Ring |
| 3 | Overwatch |
| 3 | Minecraft |
| 4 | Minecraft |
| 4 | God of War |
| 4 | Overwatch |
| 5 | NULL |
+---------------------------------+
此处提供架构 http://sqlfiddle.com/#!15/b8209c/1
CREATE TABLE games_during_trial (account_id int,game varchar);
INSERT INTO games_during_trial (account_id,game)
VALUES ('1', 'Minecraft'), ('1', 'Overwatch'), ('2', NULL), ('3', 'God of War'), ('4', 'Minecraft'), ('4', 'Elden Ring'), ('5', 'Minecraft');
CREATE TABLE games_after_trial (account_id int, game varchar);
INSERT INTO games_after_trial (account_id,game)
VALUES ('1', 'Overwatch'), ('1', 'Elden Ring'), ('2', 'Horizon'), ('2', 'Elden Ring'), ('3', 'Overwatch'), ('3', 'Minecraft'), ('4', 'Minecraft'), ('4', 'God of War'), ('4', 'Overwatch'), ('5', NULL);
我如何在 Postgresql 中加入它们,以便当有完整的比赛(account_id 和游戏)时它像往常一样加入,但是,当其中一个表中缺少游戏时,它仍然加入但放 NULL?所需的输出应如下所示:
Desired JOIN
+---------------------------------------------------------+
|account_id |games_during_trial |games_after_trial |
+---------------------------------------------------------+
| 1 | Minecraft | NULL |
| 1 | Overwatch | Overwatch |
| 1 | NULL | Elden Ring |
| 2 | NULL | Horizon |
| 2 | NULL | Elden Ring |
| 3 | God of War | NULL |
| 3 | NULL | Overwatch |
| 3 | NULL | Minecraft |
| 4 | Minecraft | Minecraft |
| 4 | Elden Ring | NULL |
| 4 | NULL | God of War |
| 4 | NULL | Overwatch |
| 5 | Minecraft | NULL |
+---------------------------------------------------------+
请帮忙。
你想要一个完整的外部连接:
SELECT COALESCE(gd.account_id, ga.account_id) AS account_id,
gd.game AS games_during_trial,
ga.game AS games_after_trial
FROM games_during_trial gd
FULL OUTER JOIN games_after_trial ga
ON gd.account_id = ga.account_id AND
gd.game = ga.game
WHERE gd.game IS NOT NULL OR ga.game IS NOT NULL
ORDER BY 1;