DATE_FORMAT显示NULL

时间:2014-01-02 17:27:22

标签: mysql sql

SQL:

SELECT 
    MAX(ansprechpartner.timestamp), 
    MAX(gallery.timestamp), 
    MAX(instrument.timestamp), 
    MAX(kurs.timestamp), 
    DATE_FORMAT('maxDate', '%d. %M %Y') as MaxDateFormat,
    CASE
        WHEN MAX(ansprechpartner.timestamp) > MAX(gallery.timestamp) 
                AND MAX(ansprechpartner.timestamp) > MAX(instrument.timestamp) 
                AND MAX(ansprechpartner.timestamp) > MAX(kurs.timestamp) 
            THEN MAX(ansprechpartner.timestamp)
        WHEN MAX(gallery.timestamp) > MAX(ansprechpartner.timestamp) 
                AND MAX(gallery.timestamp) > MAX(instrument.timestamp) 
                AND MAX(gallery.timestamp) > MAX(kurs.timestamp) 
            THEN MAX(gallery.timestamp)
        WHEN MAX(instrument.timestamp) > MAX(ansprechpartner.timestamp) 
                AND MAX(instrument.timestamp) > MAX(gallery.timestamp) 
                AND MAX(instrument.timestamp) > MAX(kurs.timestamp) 
            THEN MAX(instrument.timestamp)
        ELSE MAX(kurs.timestamp)
    END AS maxDate
FROM ansprechpartner, gallery, instrument, kurs

.timestamp列的类型为timestamp

问题是,我在NULL中获得MaxDateFormat值。知道我可能错过了什么吗?

3 个答案:

答案 0 :(得分:2)

引用列名'maxDate',导致MySQL将其视为字符串文字,无法将其转换为日期,因此结果为NULL。不加引号尝试:

DATE_FORMAT(maxDate, '%d. %M %Y') as MaxDateFormat

答案 1 :(得分:0)

您无法在定义它的同一SELECT列表中访问别名maxDate。 虽然你没有发错,因为你在qoutes中写了maxDate,使它成为一个字符串文字,预计无法转换为dateformat并返回NULL。 使用如下所示的派生表表达式

SELECT col1,col2,col3,DATE_FORMAT(maxDate, '%d. %M %Y') as MaxDateFormat,maxDate 
FROM

(   SELECT 
    MAX(ansprechpartner.timestamp) as col1, 
    MAX(gallery.timestamp) as col2, 
    MAX(instrument.timestamp) as col3, 
    MAX(kurs.timestamp) col4, 

    CASE
        WHEN MAX(ansprechpartner.timestamp) > MAX(gallery.timestamp) 
                AND MAX(ansprechpartner.timestamp) > MAX(instrument.timestamp) 
                AND MAX(ansprechpartner.timestamp) > MAX(kurs.timestamp) 
            THEN MAX(ansprechpartner.timestamp)
        WHEN MAX(gallery.timestamp) > MAX(ansprechpartner.timestamp) 
                AND MAX(gallery.timestamp) > MAX(instrument.timestamp) 
                AND MAX(gallery.timestamp) > MAX(kurs.timestamp) 
            THEN MAX(gallery.timestamp)
        WHEN MAX(instrument.timestamp) > MAX(ansprechpartner.timestamp) 
                AND MAX(instrument.timestamp) > MAX(gallery.timestamp) 
                AND MAX(instrument.timestamp) > MAX(kurs.timestamp) 
            THEN MAX(instrument.timestamp)
        ELSE MAX(kurs.timestamp)
    END AS maxDate
FROM ansprechpartner, gallery, instrument, kurs
) as z

在上面的查询中,内部查询用作派生表,通过outemost SELECT列表进行进一步处理。 为内部查询赋予一个名称as Z使其成为允许SELECT子句在其上工作的表格。

答案 2 :(得分:-2)

试试这个: -

select ansprechpartner_timestamp, gallery_timestamp, instrument_timestamp, kurs_timestamp, DATE_FORMAT(maxDate, '%d. %M %Y') as MaxDateFormat 
from 
(SELECT MAX(ansprechpartner.timestamp) as ansprechpartner_timestamp, MAX(gallery.timestamp) as gallery_timestamp, MAX(instrument.timestamp) as instrument_timestamp, MAX(kurs.timestamp) as kurs_timestamp,

CASE
WHEN MAX(ansprechpartner.timestamp) > MAX(gallery.timestamp) AND MAX(ansprechpartner.timestamp) > MAX(instrument.timestamp) AND MAX(ansprechpartner.timestamp) > MAX(kurs.timestamp) THEN MAX(ansprechpartner.timestamp)
WHEN MAX(gallery.timestamp) > MAX(ansprechpartner.timestamp) AND MAX(gallery.timestamp) > MAX(instrument.timestamp) AND MAX(gallery.timestamp) > MAX(kurs.timestamp) THEN MAX(gallery.timestamp)
WHEN MAX(instrument.timestamp) > MAX(ansprechpartner.timestamp) AND MAX(instrument.timestamp) > MAX(gallery.timestamp) AND MAX(instrument.timestamp) > MAX(kurs.timestamp) THEN MAX(instrument.timestamp)
ELSE MAX(kurs.timestamp)
END AS maxDate

FROM ansprechpartner, gallery, instrument, kurs ) as table1