0

我的查询看起来像这样:

SELECT tablea.field,
(
 SELECT sum(count) FROM
 (
      SELECT count(distinct(subtable.regdate)) as count
      FROM subtable
      where subtable.regid = tableb.regid
      GROUP BY subtable.userid

 ) as a

) as thesum
FROM tablea 
LEFT JOIN tableb on tableb.id = tablea.tableaid
GROUP BY tablea.field2

问题是在我的子查询中引用的表“tableb”是未知的,所以我收到错误:“'where 子句'中的未知列'tableb.regid'”

我该如何解决这个问题?

谢谢 !

4

1 回答 1

1

您可以将子查询移动到from子句:

SELECT tablea.field, thesum
FROM tablea left join
     tableb
     on tableb.id = tablea.tableaid left join
     (select st.regid, sum(count) as thesum
      from (SELECT st.regid, st.userid, count(distinct(st.regdate)) as count
            FROM subtable st
            GROUP BY st.regid, st.userid
           ) st
      group by st.regid
     ) st
     on st.regid = tableb.regid
GROUP BY tablea.field2;

但是,您的原始查询应该可以工作。我怀疑名字有错别字。或者tableb实际上可能在子查询中(在这种情况下,它会超出范围)或者它有一个别名,而您正在使用基表名称。

于 2013-06-17T11:18:20.300 回答