MySQL - 字段列表中的未知列

时间:2013-08-27 14:54:35

标签: mysql

我正在尝试制作一份显示计算机时代的报告。

SELECT 
M.NAME AS MACHINE_NAME, 
M.CS_MODEL AS MODEL, 
DA.SERVICE_TAG, 
DA.SHIP_DATE,
M.USER_LOGGED AS LAST_LOGGED_IN_USER, 
DW.SERVICE_LEVEL_CODE, 
DW.SERVICE_LEVEL_DESCRIPTION, 
DW.END_DATE AS EXPIRATION_DATE 
FROM MACHINE M 
JOIN DELL_ASSET DA ON (M.BIOS_SERIAL_NUMBER = DA.PARENT_SERVICE_TAG OR M.BIOS_SERIAL_NUMBER = DA.SERVICE_TAG) 
JOIN (SELECT SERVICE_TAG, SERVICE_LEVEL_CODE, SERVICE_LEVEL_DESCRIPTION, MAX(END_DATE) from DELL_WARRANTY group by SERVICE_TAG, SERVICE_LEVEL_CODE, SERVICE_LEVEL_DESCRIPTION) DW ON DW.SERVICE_TAG = DA.SERVICE_TAG 
WHERE M.CS_MANUFACTURER LIKE '%dell%' 
AND M.BIOS_SERIAL_NUMBER!='' 
AND DA.DISABLED != 1 
AND DW.END_DATE < NOW()
AND DW.SERVICE_TAG IS NULL
;

上面的代码会产生此错误。我做错了什么?

  

块引用

错误代码:1054。“字段列表”

中的未知列'DW.END_DATE'

2 个答案:

答案 0 :(得分:1)

你应该使用

MAX(END_DATE) as END_DATE

当你定义它时

答案 1 :(得分:0)

您需要在子查询中为MAX(END_DATE)计算字段添加别名。

JOIN (SELECT    SERVICE_TAG
                , SERVICE_LEVEL_CODE
                , SERVICE_LEVEL_DESCRIPTION
                , MAX(END_DATE) AS END_DATE
        from    DELL_WARRANTY 
        group by
                SERVICE_TAG
                , SERVICE_LEVEL_CODE
                , SERVICE_LEVEL_DESCRIPTION) DW 
ON  DW.SERVICE_TAG = DA.SERVICE_TAG