簡體   English   中英

MAX()不從LEFT JOIN返回NULL行

[英]MAX() doesn't return NULL rows from LEFT JOIN

這將返回多行:

SELECT w.word_id, w.word_word, w.word_visits, w.word_created_unix, b.bid_per_visit
FROM keywords_words AS w
LEFT JOIN keywords_bids AS b
ON w.word_id = b.bid_word_id WHERE w.word_word LIKE 'an%'
ORDER BY w.word_visits DESC
LIMIT 10

但這只會返回在keyword_bids中有出價的行:

SELECT w.word_id, w.word_word, w.word_visits, w.word_created_unix, MAX(b.bid_per_visit)
FROM keywords_words AS w
LEFT JOIN keywords_bids AS b
ON w.word_id = b.bid_word_id WHERE w.word_word LIKE 'an%'
ORDER BY w.word_visits DESC
LIMIT 10

如果有出價,如何獲取它以返回MAX(b.bid_per_visit)如果沒有出價,則如何返回零。

基本上不從原始LIKE搜索中排除行。

使用合並:

...
MAX(coalesce(b.bid_per_visit, 0))
...

或者不要簡單地分組:

...
coalesce(b.bid_per_visit, 0)
...

coalesce()返回其值列表中的第一個非空值。 對於左聯接,如果沒有匹配的行,則為聯接器表返回null。

IFNULL(MAX(b.bid_per_visit),0)的使用解決了您的問題,如果MAX(b.bid_per_visit)為null,則返回0,如果b.bid_per_visit不為null,則返回最大值。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM