gpt4 book ai didi

mysql - 有没有办法通过 "WITH"语句获得输出?

转载 作者:行者123 更新时间:2023-11-29 09:23:47 24 4
gpt4 key购买 nike

Question: For all countries whose government form is a 'Federal Republic' and whose official language is either 'English' or 'German', add 100,000 to their population, set their GNPOld to be equal to their current GNP, then increase their current GNP by 10,000

ERD

因此,我得到了上述 ERD 作为引用,在尝试回答这个问题时,我想知道是否需要在更新之前从表中选择列。

经过一番搜索 https://dev.mysql.com/doc/refman/8.0/en/update.html我试过这个

UPDATE country

SET Population = Population + 100000,
GNPOld = GNP,
GNP = GNP + 10000

WHERE GovernmentForm = 'Federal Republic' AND (Language = 'English' OR Language = 'German') AND IsOfficial = true;

并且

WITH GovernmentForm = 'Federal Republic' AND (Language = 'English' OR Language = 'German') AND IsOfficial = true

UPDATE country

SET ...

两者都不起作用。我猜测我的代码结构对于这种查询来说是错误的。指针或提示将不胜感激。谢谢!

最佳答案

您必须加入countryLanguage才能获取LanguageIsOfficial列。

UPDATE country AS c
JOIN CountryLanguage AS cl ON c.Code = cl.CountryCode
SET c.Population = c.Population + 100000,
c.GNPOld = c.GNP,
c.GNP = c.GNP + 10000
WHERE c.GovernmentForm = 'Federal Republic'
AND cl.Language IN ('English', 'German')
AND cl.IsOfficial = 'T';

此外,IsOfficial 是一个 ENUM,而不是 bool 值。我建议使用 IN() 来测试几种可能性之一,而不是 OR

关于mysql - 有没有办法通过 "WITH"语句获得输出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59977288/

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