gpt4 book ai didi

mysql - 无法使用 JOIN 更新特定记录

转载 作者:太空宇宙 更新时间:2023-11-03 11:24:57 25 4
gpt4 key购买 nike

我正在尝试使用 JOIN 更新特定的竞争的结构如下:

国家:

id | name | iso
5 Brazil BR

竞争:

id | country_id | name     |
1 5 Serie A
2 5 Serie B
3 5 Serie C

competition_seasons:

id | competition_id | name  | update_at
1 1 2019 2019-01-15 00:00:00
2 1 2018 2019-01-15 00:00:00
3 1 2017 2019-01-15 00:00:00
4 2 2019 2019-01-15 00:00:00
5 2 2018 2019-01-15 00:00:00
6 2 2017 2019-01-15 00:00:00
7 3 2019 2019-01-15 00:00:00
8 3 2018 2019-01-15 00:00:00
9 3 2017 2019-01-15 00:00:00

目标是将 competition_seasons 上的 update_at 设置为 null。所以我写了这个查询:

UPDATE country n
JOIN competition c ON n.id = c.country_id
JOIN competition_seasons s ON c.id = s.competition_id
SET s.update_at = NULL
WHERE n.name = "Brazil"

问题是查询没有更新任何东西。我做错了什么?

最佳答案

您应该在查询中首先列出您打算更新的表。在这种情况下,我们可以反转连接的顺序,首先列出 competition_seasons 表:

UPDATE competition_seasons s     -- target table, listed first
INNER JOIN competition c
ON c.id = s.competition_id
INNER JOIN country n
ON n.id = c.country_id
SET
s.update_at = NULL
WHERE
n.name = 'Brazil';

请注意,这里连接的顺序无关紧要,假设它们都是内部连接。

关于mysql - 无法使用 JOIN 更新特定记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55160611/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com