我有一个看起来像这样的子查询:
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'
那么如何从结果中删除它们呢?
答案 0 :(得分:0)
可以在GROUP BY,ORDER BY或HAVING子句中使用列别名。
参考mysql文档。
http://dev.mysql.com/doc/refman/5.0/en/problems-with-alias.html
答案 1 :(得分: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