错误代码:1054。“子句”中的未知列

时间:2019-06-08 08:15:59

标签: mysql

该查询旨在根据一个表中的值除以另一表的值来返回百分比。但是,出了点问题,我想念它。

板上出现的类似问题看起来与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);

表结构: 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子句以仅测试逻辑和语法。

感谢您的帮助。

3 个答案:

答案 0 :(得分:0)

您已将表别名化为sustainerPop和locationpop,因此 您需要在ON子句

中使用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;

以及外部FROM(..)T的表别名

答案 1 :(得分:0)

我没有要测试的数据库,所以我不是100%肯定会运行该数据库,但是下面的查询会不会更简单一些?

'chromedriverVersion': '74.0.3729.6'

我假设每个表中每个位置只有一个位置记录?

答案 2 :(得分:0)

查询中存在问题。由于apinfectcount.APInfectCountLocation列和apcountrypop.apcountrypopCountry列的范围仅限于子查询,因此您不能在子查询之外(在where子句中)使用它。 您可以在子查询https://docs.microsoft.com/en-us/sql/relational-databases/performance/subqueries?view=sql-server-2017

上查看这些文档

请参阅下面的代码。

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);