字段列表中的未知表

时间:2013-06-21 23:30:58

标签: mysql sql

我正在尝试构建一个看起来相当简单的嵌套查询,但是当我将以下两个工作查询结合起来时,我收到以下错误:

字段列表中的未知表'cm' ErrorNr。 = 1109。

SELECT `cm`.`cans_id`   AS `cans_id`,
       `cm`.`cans_date` AS `cans_date`, 
       `cm`.`begintfin` AS `begIntFin`, 
       `c`.`cans_id`    AS `CAN_ID`, 
       `c`.`q001`       AS `q001`, 
       `c`.`q002`       AS `q002`, 
       `c`.`q093`       AS `q093`,
       `c`.`q094`       AS `q094`,
       `c`.`mru`        AS `mru2`,
       (SELECT Count(0) AS `count(*)`
        FROM   `cans2notes` `nt`
               JOIN `cans2meta` `cm`
                 ON `cm`.`cans_id` = `nt`.`cans_id`
               JOIN `cans2surveys` `c`
                 ON `c`.`cans_id` = `cm`.`cans_id`
        WHERE  ( `nt`.`cans_id` = `c`.`cans_id` ));

对此的任何想法都将非常感激。

1 个答案:

答案 0 :(得分:2)

您指的是一个表,但没有FROM或WHERE子句。

基本上你有

select col,
       col,
       col,
       col,
       (select count(*) from sometable)

但你没有

 FROM table cm
 INNER JOIN table c
 ON CM.ID = C.OTHER_ID

我猜你要做的是这个:

SELECT `cm`.`cans_id`   AS `cans_id`,
       `cm`.`cans_date` AS `cans_date`, 
       `cm`.`begintfin` AS `begIntFin`, 
       `c`.`cans_id`    AS `CAN_ID`, 
       `c`.`q001`       AS `q001`, 
       `c`.`q002`       AS `q002`, 
       `c`.`q093`       AS `q093`,
       `c`.`q094`       AS `q094`,
       `c`.`mru`        AS `mru2`,
       `count(*)`       AS 'count'
        FROM   `cans2notes` `nt`
               JOIN `cans2meta` `cm`
                 ON `cm`.`cans_id` = `nt`.`cans_id`
               JOIN `cans2surveys` `c`
                 ON `c`.`cans_id` = `cm`.`cans_id`
        WHERE  ( `nt`.`cans_id` = `c`.`cans_id` )
        GROUP BY `cm`.`cans_id`,
       `cm`.`cans_date`, 
       `cm`.`begintfin`, 
       `c`.`cans_id`, 
       `c`.`q001`, 
       `c`.`q002`, 
       `c`.`q093`,
       `c`.`q094`,
       `c`.`mru`;