SQL Query给出了未知列的错误

时间:2014-06-23 11:54:50

标签: mysql sql

我正在用Power Meter Analysis的PHP创建一个应用程序。我有以下表结构:

表格:' Feed'

| feed_id | device_no | current1 | voltage1 | power_factor_1 | vc1 | ic1 | date_added
-------------------------------------------------------------------------------------
| 36752   | 2         | 36.048   | 196.01   | 0.9            | 1   | 1   | 2014-06-23 14:14:44
| 36753   | 2         | 35.963   | 195.59   | 0.9            | 1   | 1   | 2014-06-23 14:15:34

等等。

表:'机器'

| machine_id | machine_phone | machine_name | company_id |
----------------------------------------------------------
| 1          | 2             | ABC Machine  | 1          |
| 2          | 093           | DEF Machine  | 1          |

我需要每小时记录一次,为此我写了以下查询:

$sql =  "
            SELECT
                SUM(t.power1) AS 'power1'
                , HOUR(t.date) AS 'pulse_hour' 

            FROM    (
                SELECT
                    IF(@diff = 0, 0, (((f.voltage1*f.vc1)*(f.current1*f.ic1)*(f.power_factor_1))/1000) * (@diff/3600)) AS 'power1'
                    , IF(@diff = 0,0, TIME_TO_SEC(f.date_added) - @diff) AS 'deltaT'
                    , @diff := TIME_TO_SEC(f.date_added)
                    , f.date_added AS 'date'
                FROM
                    feeds f,
                    (SELECT @diff := 0) AS X
                left join
                    machine m
                on
                    f.device_no = m.machine_phone
                left join
                    company c
                on
                    c.company_id = m.company_id
            ";

    $sql .= $params['machine_id'] ? " where f.device_no = '".$params['machine_id']."'" : " where f.device_no > 0";
    $sql .= $params['machine_pulse_datetime_from'] ? " and f.date_added >= '".$params['machine_pulse_datetime_from']."'" : "";
    $sql .= $params['machine_pulse_datetime_to'] ? " and f.date_added <= '".$params['machine_pulse_datetime_to']."'" : "";
    $sql .= $params['company_id'] ? " and c.company_id = '".$params['company_id']."'" : "";

    $sql .= "
                ORDER BY
                    f.date_added ASC
                ) t
            GROUP BY HOUR(t.date)
            ORDER BY HOUR(t.date) ASC

            ";

如果从查询中删除以下部分,则查询运行正常:

left join
                    machine m
                on
                    f.device_no = m.machine_phone
                left join
                    company c
                on
                    c.company_id = m.company_id

但是这部分它给了我以下错误:

Error Code : 1054
Unknown column 'f.device_no' in 'on clause'

你可以帮我解决这个问题吗...我花了一个小时来处理这个问题:(

1 个答案:

答案 0 :(得分:3)

这是您的join

         FROM feeds f,
              (SELECT @diff := 0) AS X left join
              machine m
              on f.device_no = m.machine_phone left join
              company c
              on c.company_id = m.company_id

问题在于您正在混合显式和隐式连接。您可以将逗号替换为cross join

来解决此问题
        FROM feeds f cross join
              (SELECT @diff := 0) AS X left join
              machine m
              on f.device_no = m.machine_phone left join
              company c
              on c.company_id = m.company_id

这个问题深深埋藏在select的文档中,,cross join非常相似,但范围规则除外 - 也就是说,表别名被识别。使用逗号,表别名无法按预期识别。它们带有cross join

Here是参考:

  

INNER [CROSS] JOIN和(逗号)在语义上是等效的   没有连接条件:两者之间产生笛卡尔积   指定的表(即第一个表中的每一行)   连接到第二个表中的每一行。)

     

但是,逗号运算符的优先级小于INNER JOINCROSS JOINLEFT JOIN,依此类推。如果你混合逗号连接   当存在连接条件时,使用其他连接类型,错误为   表格未知栏&#39; col_name&#39;在&#39; on条款&#39;可能导致。   有关处理此问题的信息将在后面给出   部分。