mysql - 错误代码 : 1054. 'sdate' 中的未知列 'where clause'

标签 mysql sql select union-all

我在查询中遇到此错误,您知道如何将 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 列.

最佳答案

我没有在主 select 子句中进行选择,而是创建了一个子查询来连接,以便可以检查 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

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

关于mysql - 错误代码 : 1054. 'sdate' 中的未知列 'where clause',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54927792/

相关文章:

mysql - 自内连接获取单条记录

mysql - 如何使用查询从数据库的 ip 范围之间选择 ip 地址?

linq - 使用 LINQ 从表中仅获取一个值

PHP mysql 查询生成器显示我想要的内容

MySQL 将行转为动态数量的列

php - 用户登录页面 PHP 的 if 语句出错

mysql - 如何最大限度地减少这些子查询的使用并提高性能?

java - Netbeans:包 com.mysql 不存在

php - 如何将两个表格行放在一起

javascript - 将三个选择菜单中的元素切入或切出数组