Mysql 错误代码:1054。'中的未知列;关于第'条;
查询的目的是返回一个表的值除以另一个表的值的百分比。然而,有些地方出了问题,我错过了 董事会上提到的类似问题看起来与JOIN有关,但似乎不是问题,当我尝试显式JOIN时——基本上mysql就像——现在你是个白痴——我一定是做错了,或者这不是问题所在Mysql 错误代码:1054。'中的未知列;关于第'条;,mysql,Mysql,查询的目的是返回一个表的值除以另一个表的值的百分比。然而,有些地方出了问题,我错过了 董事会上提到的类似问题看起来与JOIN有关,但似乎不是问题,当我尝试显式JOIN时——基本上mysql就像——现在你是个白痴——我一定是做错了,或者这不是问题所在 SELECT (pathogenPop / locationpop) as PercentInfected FROM ( (SELECT apinfectcount.APInfectCountInfected
SELECT (pathogenPop / locationpop) as PercentInfected
FROM (
(SELECT apinfectcount.APInfectCountInfected
as pathogenPop, apinfectcount.APInfectCountLocation
FROM apstart.apinfectcount
GROUP BY apinfectcount.APInfectCountLocation) as pathogenPop
Inner JOIN
(SELECT apcountrypop.apcountrypopPopulation
as locationpop, apcountrypop.apcountrypopCountry
FROM apstart.apcountrypop
GROUP BY apcountrypop.apcountrypopCountry)
as locationpop
on apinfectcount.APInfectCountLocation = apcountrypop.apcountrypopCountry
and apinfectcount.APInfectCountWeek = 23);
表架构:apcountrypop
idapcountrypop INT(11)
apcountrypopCountry VarChar(45)
apcountrypopPopulation FLOAT
表架构:apinfectcount
idAPInfectCount INT(11)
APInfectCountLocation VarChar(45)
APInfectCountOutBreak VarChar(45)
APInfectCountPathogen VarChar(45)
APInfectCountInfected FLOAT
APInfectCountDead FLOAT
APInfectCountWeek VarChar(45)
如果成功了--
它会将apinfectcount.APInfectCountInfected分配给POP
和apcountrypop.apcountrypop人口到locationpop
对于位置相同的值(apinfectcount.APInfectCountLocation=apcountrypop.apcountrypopCountry)
然后它将返回apinfectcount表的值,该值除以apcountrypop表以给出百分比
所以在这个特定的例子中,我只有样本数据,所以我只想返回一个值,所以我添加了where子句来测试逻辑和语法
非常感谢您的帮助。您已将表别名设为POP和locationpop 您需要
pop.APInfectCountLocation=locationpop.apcountrypopCountry
ON子句中的pop.APInfectCountWeek=23
SELECT (pathogenPop / locationpop) as PercentInfected
FROM (
(SELECT apinfectcount.APInfectCountInfected
as pathogenPop, apinfectcount.APInfectCountLocation
FROM apstart.apinfectcount
GROUP BY apinfectcount.APInfectCountLocation) as pathogenPop
Inner JOIN
(SELECT apcountrypop.apcountrypopPopulation
as locationpop, apcountrypop.apcountrypopCountry
FROM apstart.apcountrypop
GROUP BY apcountrypop.apcountrypopCountry)
as locationpop
on pathogenPop.APInfectCountLocation = locationpop.apcountrypopCountry
and pathogenPop.APInfectCountWeek = 23) T;
还有一个表别名,用于来自(..)T的外部 您需要
pop.APInfectCountLocation=locationpop.apcountrypopCountry
ON子句中的pop.APInfectCountWeek=23
SELECT (pathogenPop / locationpop) as PercentInfected
FROM (
(SELECT apinfectcount.APInfectCountInfected
as pathogenPop, apinfectcount.APInfectCountLocation
FROM apstart.apinfectcount
GROUP BY apinfectcount.APInfectCountLocation) as pathogenPop
Inner JOIN
(SELECT apcountrypop.apcountrypopPopulation
as locationpop, apcountrypop.apcountrypopCountry
FROM apstart.apcountrypop
GROUP BY apcountrypop.apcountrypopCountry)
as locationpop
on pathogenPop.APInfectCountLocation = locationpop.apcountrypopCountry
and pathogenPop.APInfectCountWeek = 23) T;
还有一个表别名,用于外部FROM(..)T我没有要测试的数据库,因此我不能100%确定这将运行,但是下面的查询会不会更简单一些
SELECT (apinfectcount.APInfectCountInfected / apcountrypop.apcountrypopPopulation) as PercentInfected, apinfectcount.APInfectCountLocation
FROM apinfectcount
INNER JOIN apcountrypop ON apcountrypop.apcountrypopCountry = apinfectcount.APInfectCountLocation
WHERE apinfectcount.APInfectCountWeek = 23
GROUP BY apinfectcount.APInfectCountLocation
我假设每个表中的每个位置只有一个位置记录?我没有要测试的数据库,所以我不能100%确定这会运行,但是下面的查询会不会更简单一些
SELECT (apinfectcount.APInfectCountInfected / apcountrypop.apcountrypopPopulation) as PercentInfected, apinfectcount.APInfectCountLocation
FROM apinfectcount
INNER JOIN apcountrypop ON apcountrypop.apcountrypopCountry = apinfectcount.APInfectCountLocation
WHERE apinfectcount.APInfectCountWeek = 23
GROUP BY apinfectcount.APInfectCountLocation
我假设每个表中每个位置只有一个位置记录?查询中存在问题。由于apinfectcount.APInfectCountLocation列和apcountrypop.apcountrypopCountry列的作用域仅限于子查询,因此您不能在子查询之外(在where子句内)使用它。 您可以在子查询中签出这些文档 请参阅下面的代码
SELECT (countInfected / countrypopulation) as PercentInfected
FROM (
(SELECT apinfectcount.APInfectCountInfected
as countinfected, apinfectcount.APInfectCountLocation, APInfectCountWeek as
countweek
FROM apstart.apinfectcount
GROUP BY apinfectcount.APInfectCountLocation) as pathogenPop
Inner JOIN
(SELECT apcountrypop.apcountrypopPopulation
as countrypopulation, apcountrypop.apcountrypopCountry
FROM apstart.apcountrypop
GROUP BY apcountrypop.apcountrypopCountry)
as locationpop
on pathogenPop.countinfected = locationpop.countrypopulation
and pathogenPop.countweek= 23);
查询中存在问题。由于apinfectcount.APInfectCountLocation列和apcountrypop.apcountrypopCountry列的作用域仅限于子查询,因此您不能在子查询之外(在where子句内)使用它。 您可以在子查询中签出这些文档 请参阅下面的代码
SELECT (countInfected / countrypopulation) as PercentInfected
FROM (
(SELECT apinfectcount.APInfectCountInfected
as countinfected, apinfectcount.APInfectCountLocation, APInfectCountWeek as
countweek
FROM apstart.apinfectcount
GROUP BY apinfectcount.APInfectCountLocation) as pathogenPop
Inner JOIN
(SELECT apcountrypop.apcountrypopPopulation
as countrypopulation, apcountrypop.apcountrypopCountry
FROM apstart.apcountrypop
GROUP BY apcountrypop.apcountrypopCountry)
as locationpop
on pathogenPop.countinfected = locationpop.countrypopulation
and pathogenPop.countweek= 23);
让MySQL报告更多关于错误的信息,您就会知道是什么列名或缺少的名称导致了问题。感谢您的回复!此问题已修复,但仍不起作用。。。我们将做更多的研究来修复它。完成后,我将发布更新的代码。我真的非常感谢你的帮助!大家好,下面所有的回答都有效。我发现了为什么它没有返回结果的问题。。。。全部新秀动作。数据库中有一个拼写错误。哈哈,没有你我就无法修好。再次感谢!让MySQL报告更多关于错误的信息,您就会知道是什么列名或缺少的名称导致了问题。感谢您的回复!此问题已修复,但仍不起作用。。。我们将做更多的研究来修复它。完成后,我将发布更新的代码。我真的非常感谢你的帮助!大家好,下面所有的回答都有效。我发现了为什么它没有返回结果的问题。。。。全部新秀动作。数据库中有一个拼写错误。哈哈,没有你我就无法修好。再次感谢!让我试试这个。非常感谢您的回复!实际上--在apinfectcount.APInfectCountLocation中,有许多记录具有相同的位置。我需要将其指定为外键吗?很抱歉,我还不知道如何格式化注释--但这是一个很好的建议。我做了一些编辑,效果很好。仍然没有得到结果,所以我不得不花更多的时间来研究它,但你的建议奏效了。选择(apinfectcount.APInfectCountInfected/apcountrypop.apcountrypopulation)作为PercentInfected,apinfectcount.APInfectCountLocation从apstart.apinfectcount内部加入apcountrypop上的apstart.apcountrypop.apcountrypopCountry=apinfectcount.APInfectCountLocation,其中apinfectcount.APInfectCountWeek=23按apinfectcount.APInfectCountLocation分组;如果一个位置有多条记录,则需要对所选字段求和()并按其分组。注释对于将代码选择(SUM(apinfectcount.APInfectCountInfected)/SUM(apcountrypop.apcountrypopulation))格式化为PercentInfected不是很好,apinfectcount.APInfectCountLocation从apinfectcount内部加入apcountrypop到apcountrypop.apcountrypopCountry=apinfectcount.APInfectCountLocation其中apinfectcount.APInfectCountWeek=23按apinfectcount.APInfectCountLocation分组,apcountrypop.ApCountryPopCountryPopCountry让我试试这个。非常感谢您的回复!实际上--在apinfectcount.APInfectCountLocation中,有许多记录具有相同的位置。我需要将其指定为外键吗?很抱歉,我还不知道如何格式化注释--但这是一个很好的建议。我做了一些编辑,效果很好。仍然没有得到结果,所以我不得不花更多的时间来研究它,但你的建议奏效了。选择(apinfectcount.APInfectCountInfected/apcountrypop.apcountrypopulation)作为PercentInfected,apinfectcount.APInfectCountLocation从apstart.apinfectcount内部加入apcountrypop上的apstart.apcountrypop.apcountrypopCountry=apinfectcount.APInfectCountLocation,其中apinfectcount.APInfectCountWeek=23按apinfectcount.APInfectCountLocation分组;如果一个位置有多条记录,则需要