使用LEFT JOIN子查询中主表中的字段

时间:2014-12-22 07:51:15

标签: mysql sql

我想通过帮助日历表选择值。查询如下所示:

SELECT
    t1.starttime,
    t1.endtime,
    t2.sum_val1,
    t2.avg_val2
FROM
    eco_546d9dff955b5_help t1
LEFT JOIN (
    SELECT
        SUM(val1) AS sum_val1,
        AVG(val2) AS avg_val2
    FROM
        eco_546d9dff955b5
    WHERE
        `timestamp` > t1.starttime
    AND `timestamp` <= t1.endtime
) t2 ON t1.starttime <= t2.`timestamp` AND t1.endtime >= t2.`timestamp`

但是在运行此代码时,我会在where子句&#34;中得到&#34;未知列t1.starttime。是否可以使t1.starttime可用于子查询?

0 个答案:

没有答案