2014-01-10 191 views
0

我有一個子查詢,看起來像這樣使用子查詢:在where子句

SELECT field1, (
     SELECT date2 
     FROM table2 
     ORDER BY date2 DESC 
    ) AS last_contacted 
    FROM table1 
    ORDER BY last_contacted DESC 

但是,我得到記錄,其中last_contacted爲NULL。我不希望這些結果中,所以我認爲WHERE last_contacted IS NOT NULL但返回錯誤:

Unknown column 'last_contacted' in 'where clause'

所以,我怎麼能刪除那些從結果?

+2

用'HAVING'替換'WHERE' – Mihai

回答

0
SELECT field1, (
    SELECT date2 
    FROM table2 
    ORDER BY date2 DESC 
) AS last_contacted 
FROM table1 
HAVING last_contacted NOT NULL 
ORDER BY last_contacted DESC