0

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

字段列表 ErrorNr 中的未知表“cm”。= 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` ));

对此的任何想法将不胜感激。

4

1 回答 1

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`;
于 2013-06-21T23:32:52.000 回答