Where子句中的未知列

时间:2011-01-05 12:20:59

标签: mysql sql mysql-error-1054

为什么MySQL声称找不到列?我确实在'select子句'中指定了它。

MySQL 5.1.39

错误代码: 1054 'where子句'

中的未知列' dt'

查询:

-- Generate a Table for Every Day in 2008 Year
SELECT DATE_ADD('2008-01-01',
            INTERVAL (ones.num + tens.num + hundreds.num) DAY) dt
FROM
    (SELECT 0 num UNION ALL
     SELECT 1 num UNION ALL
     SELECT 2 num UNION ALL
     SELECT 3 num UNION ALL
     SELECT 4 num UNION ALL
     SELECT 5 num UNION ALL
     SELECT 6 num UNION ALL
     SELECT 7 num UNION ALL
     SELECT 8 num UNION ALL
     SELECT 9 num) ones
CROSS JOIN
    (SELECT 0 num UNION ALL
     SELECT 10 num UNION ALL
     SELECT 20 num UNION ALL
     SELECT 30 num UNION ALL
     SELECT 40 num UNION ALL
     SELECT 50 num UNION ALL
     SELECT 60 num UNION ALL
     SELECT 70 num UNION ALL
     SELECT 80 num UNION ALL
     SELECT 90 num) tens
CROSS JOIN
    (SELECT 0 num UNION ALL
     SELECT 100 num UNION ALL
     SELECT 200 num UNION ALL
     SELECT 300 num) hundreds
WHERE YEAR(dt) < 2009
ORDER BY dt;

3 个答案:

答案 0 :(得分:3)

您必须在HAVING子句中引用列别名。在处理查询之后,对结果集执行HAVING条件,而不是作为初始查询执行的一部分的WHERE子句。因此HAVING能够对列别名进行操作,这些列别名仅在结果集中可用。

-- Generate a Table for Every Day in 2008 Year
SELECT DATE_ADD('2008-01-01',
            INTERVAL (ones.num + tens.num + hundreds.num) DAY) dt
FROM
    (SELECT 0 num UNION ALL
     SELECT 1 num UNION ALL
     SELECT 2 num UNION ALL
     SELECT 3 num UNION ALL
     SELECT 4 num UNION ALL
     SELECT 5 num UNION ALL
     SELECT 6 num UNION ALL
     SELECT 7 num UNION ALL
     SELECT 8 num UNION ALL
     SELECT 9 num) ones
CROSS JOIN
    (SELECT 0 num UNION ALL
     SELECT 10 num UNION ALL
     SELECT 20 num UNION ALL
     SELECT 30 num UNION ALL
     SELECT 40 num UNION ALL
     SELECT 50 num UNION ALL
     SELECT 60 num UNION ALL
     SELECT 70 num UNION ALL
     SELECT 80 num UNION ALL
     SELECT 90 num) tens
CROSS JOIN
    (SELECT 0 num UNION ALL
     SELECT 100 num UNION ALL
     SELECT 200 num UNION ALL
     SELECT 300 num) hundreds
HAVING YEAR(dt) < 2009
ORDER BY dt;

答案 1 :(得分:1)

没有group by是特定于mysql的。你也可以尝试这样的事情:

select * from (
SELECT DATE_ADD('2008-01-01',
      INTERVAL (ones.num + tens.num + hundreds.num) DAY) dt
FROM
    (SELECT 0 num UNION ALL
     SELECT 1 num UNION ALL
     SELECT 2 num UNION ALL
     SELECT 3 num UNION ALL
     SELECT 4 num UNION ALL
     SELECT 5 num UNION ALL
     SELECT 6 num UNION ALL
     SELECT 7 num UNION ALL
     SELECT 8 num UNION ALL
     SELECT 9 num) ones
CROSS JOIN
    (SELECT 0 num UNION ALL
     SELECT 10 num UNION ALL
     SELECT 20 num UNION ALL
     SELECT 30 num UNION ALL
     SELECT 40 num UNION ALL
     SELECT 50 num UNION ALL
     SELECT 60 num UNION ALL
     SELECT 70 num UNION ALL
     SELECT 80 num UNION ALL
     SELECT 90 num) tens
CROSS JOIN
    (SELECT 0 num UNION ALL
     SELECT 100 num UNION ALL
     SELECT 200 num UNION ALL
     SELECT 300 num) hundreds
dates
where year(dates.dt) < 2009 
ORDER BY dt;

答案 2 :(得分:0)

使用这些命名列可能有点棘手,但您可以使用'HAVING'而不是WHERE:

SELECT DATE_ADD('2008-01-01',
      INTERVAL (ones.num + tens.num + hundreds.num) DAY) dt
FROM
    (SELECT 0 num UNION ALL
     SELECT 1 num UNION ALL
     SELECT 2 num UNION ALL
     SELECT 3 num UNION ALL
     SELECT 4 num UNION ALL
     SELECT 5 num UNION ALL
     SELECT 6 num UNION ALL
     SELECT 7 num UNION ALL
     SELECT 8 num UNION ALL
     SELECT 9 num) ones
CROSS JOIN
    (SELECT 0 num UNION ALL
     SELECT 10 num UNION ALL
     SELECT 20 num UNION ALL
     SELECT 30 num UNION ALL
     SELECT 40 num UNION ALL
     SELECT 50 num UNION ALL
     SELECT 60 num UNION ALL
     SELECT 70 num UNION ALL
     SELECT 80 num UNION ALL
     SELECT 90 num) tens
CROSS JOIN
    (SELECT 0 num UNION ALL
     SELECT 100 num UNION ALL
     SELECT 200 num UNION ALL
     SELECT 300 num) hundreds
HAVING YEAR(dt) < 2009
ORDER BY dt;