[英]Error Code: 1054. Unknown column in 'on clause'
The query is meant to return the percentage based on value from one table being divided by the value of another table. 该查询旨在根据一个表的值除以另一表的值返回百分比。 However, there is something wrong and I am missing it. 但是,出了点问题,我想念它。
similar problems noted on the board looked related to JOIN, but did not seem to be the problem, when I tried and explicit join -- basically mysql was like -- now you are an idiot-- I must have did that wrong or that is not the problem. 董事会上指出的类似问题看起来与JOIN有关,但似乎不是问题,当我尝试显式连接时-基本上mysql就像-现在你是个白痴-我一定做错了,或者那是没问题。
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);
Table Schema: apcountrypop 表模式: apcountrypop
idapcountrypop INT(11)
apcountrypopCountry VarChar(45)
apcountrypopPopulation FLOAT
Table Schema: apinfectcount 表架构: apinfectcount
idAPInfectCount INT(11)
APInfectCountLocation VarChar(45)
APInfectCountOutBreak VarChar(45)
APInfectCountPathogen VarChar(45)
APInfectCountInfected FLOAT
APInfectCountDead FLOAT
APInfectCountWeek VarChar(45)
If it worked -- it would assign apinfectcount.APInfectCountInfected to pathogenPop and apcountrypop.apcountrypopPopulation to locationpop 如果可行-它将apinfectcount.APInfectCountInfected分配给病原体Pop和apcountrypop.apcountrypopPopulation到locationpop
for the values where the locations are the same(apinfectcount.APInfectCountLocation = apcountrypop.apcountrypopCountry) 对于位置相同的值(apinfectcount.APInfectCountLocation = apcountrypop.apcountrypopCountry)
then it would return the value of the apinfectcount table value is divided by the apcountrypop table to give the percentage. 那么它将返回apinfectcount表的值除以apcountrypop表的值以得出百分比。
so in this specific example I only have sample data so I am just wanted to return one value so I added the where clause to just test the logic and syntax. 因此,在此特定示例中,我仅具有示例数据,因此只想返回一个值,因此添加了where子句以仅测试逻辑和语法。
I appreciate the help. 感谢您的帮助。
You have assugned the tables alias pathogenPop and locationpop so you need pathogenPop.APInfectCountLocation = locationpop.apcountrypopCountry and pathogenPop.APInfectCountWeek = 23
in ON clause 您已将表别名设置为authenticatePop和locationpop,因此您需要ON子句中的disorderPop.APInfectCountLocation pathogenPop.APInfectCountLocation = locationpop.apcountrypopCountry and pathogenPop.APInfectCountWeek = 23
disorderPop.APInfectCountWeek pathogenPop.APInfectCountLocation = locationpop.apcountrypopCountry and pathogenPop.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;
and also a table alias for the outer FROM(..) T 以及外部FROM(..)T的表别名
I don't have the database to test against so I'm not 100% certain this will run, but would the following query not be a bit simpler? 我没有要测试的数据库,所以我不是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
And I assume there is only one location record per location in each table? 我假设每个表中每个位置只有一个位置记录?
There is an issue within a query. 查询中存在问题。 As scope of apinfectcount.APInfectCountLocation column and apcountrypop.apcountrypopCountry column is limited to subquery only you cannot use it outside the subquery (within where clause). 由于apinfectcount.APInfectCountLocation列和apcountrypop.apcountrypopCountry列的范围仅限于子查询,因此您不能在子查询之外(在where子句中)使用它。 You can check out these docs on subquery https://docs.microsoft.com/en-us/sql/relational-databases/performance/subqueries?view=sql-server-2017 您可以在子查询https://docs.microsoft.com/zh-cn/sql/relational-databases/performance/subqueries?view=sql-server-2017上签出这些文档
Refer code below. 请参考下面的代码。
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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.