MAX() 不会 return 来自 LEFT JOIN 的 NULL 行
MAX() doesn't return NULL rows from LEFT JOIN
这将 return 多行:
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
但这只会在 keywords_bids 中有出价的 return 行:
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
如果有出价,我如何将其发送到 return MAX(b.bid_per_visit)
,如果没有任何出价,则为零。
基本上不排除原始 LIKE 搜索中的行。
使用合并:
...
MAX(coalesce(b.bid_per_visit, 0))
...
或者不要简单地分组:
...
coalesce(b.bid_per_visit, 0)
...
coalesce()
returns 其值列表中的第一个非空值。对于左联接,如果没有匹配的行,将为联接器返回空值 table。
使用 IFNULL(MAX(b.bid_per_visit),0) 解决您的问题,如果 MAX(b.bid_per_visit) 为空且 return 最大值为 return 0 b.bid_per_visit 如果它不为空。
这将 return 多行:
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
但这只会在 keywords_bids 中有出价的 return 行:
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
如果有出价,我如何将其发送到 return MAX(b.bid_per_visit)
,如果没有任何出价,则为零。
基本上不排除原始 LIKE 搜索中的行。
使用合并:
...
MAX(coalesce(b.bid_per_visit, 0))
...
或者不要简单地分组:
...
coalesce(b.bid_per_visit, 0)
...
coalesce()
returns 其值列表中的第一个非空值。对于左联接,如果没有匹配的行,将为联接器返回空值 table。
使用 IFNULL(MAX(b.bid_per_visit),0) 解决您的问题,如果 MAX(b.bid_per_visit) 为空且 return 最大值为 return 0 b.bid_per_visit 如果它不为空。