【问题标题】:Error Code: 1054. Unknown column 'sdate' in 'where clause'错误代码:1054。“where 子句”中的未知列“sdate”
【发布时间】:2019-02-28 14:20:09
【问题描述】:

我的查询中出现此错误,您知道如何将sdate 放在 2 层子查询中吗?

select
at.startDate as sdate, at.dau as DAU,
(
select count(distinct d.uid) from
 (select ses.uid from dsession as ses where ses.startDate = sdate group by ses.uid
  union all
  select res.uid from rsession as res where res.startDate = sdate group by res.uid) as te
) as MAU, (SELECT DAU/MAU) as AVG
from
attendance as at 

如果我单独查询子查询,它可以工作,但是当我将它合并到主查询时,sdate 变得未知。有什么想法吗?

我尝试将where 上的sdate 替换为at.startDate,但仍然得到未知的at.startDate 列。

【问题讨论】:

    标签: mysql sql select union-all


    【解决方案1】:

    我没有在主选择子句中进行选择,而是创建了一个子查询来加入,以便可以检查 startDate

    SELECT at.startDate AS sdate, at.dau AS DAU, (DAU/MAU.cnt) as AVG
    FROM attendance AS at
    JOIN (SELECT startdate, count(distinct uid) as cnt
          FROM (SELECT uid, startdate FROM dsession
                UNION ALL
                SELECT uid, startdate FROM rsession) as ua
          GROUP BY startdate
         ) as MAU ON MAU.startdate = at.startdate
    

    希望我在重组查询时没有搞砸任何事情:)

    【讨论】:

      【解决方案2】:

      您不能在where 子句中使用列别名。只需使用原始列名:

      where at.startDate between @startDate and @endDate 
      

      order by 中接受了别名,因此不必更改。

      【讨论】:

      • 我发誓我几乎看到你的答案弹出之前问题完成加载+1。
      • 子查询嵌套了两次。 at 在该范围内未知。
      猜你喜欢
      • 2016-03-19
      • 2020-11-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多