当我期望NULL
0
值NULL
时,此子查询返回SELECT
(SELECT ifnull(visit.timestamp, 0) AS ifnull_1
FROM keyword
WHERE visit.keyword_id = keyword.id
ORDER BY visit.timestamp DESC LIMIT 1) AS keyword_last_visit
FROM keyword
LEFT OUTER JOIN visit ON visit.keyword_id = keyword.id
的任何想法:
keyword_last_visit
NULL
始终返回{{1}}
答案 0 :(得分:1)
使用CASE
SELECT
(SELECT (CASE WHEN visit.timestamp IS NULL THEN 0 ELSE visit.timestamp END) AS ifnull_1
FROM keyword
WHERE visit.keyword_id = keyword.id
ORDER BY visit.timestamp DESC LIMIT 1) AS keyword_last_visit
FROM keyword
LEFT OUTER JOIN visit ON visit.keyword_id = keyword.id
答案 1 :(得分:0)
这应该用在外部查询..
SELECT
ifnull((SELECT ifnull(visit.timestamp, 0) AS ifnull_1
FROM keyword
WHERE visit.keyword_id = keyword.id
ORDER BY visit.timestamp DESC LIMIT 1), 0) AS keyword_last_visit
FROM keyword
LEFT OUTER JOIN visit ON visit.keyword_id = keyword.id