This will return multiple rows:
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
But this will only return rows where there is a bid in keywords_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
How do I get it to return the MAX(b.bid_per_visit)
if there is a bid, and zero if there isn't any bids.
Not excluding rows from the original LIKE search basically.
Use coalesce:
...
MAX(coalesce(b.bid_per_visit, 0))
...
Or don't group by and simply:
...
coalesce(b.bid_per_visit, 0)
...
coalesce()
returns the first non-null value in its list of values. With left joins, nulls are returned for the joiner table is there's no matching row.
IFNULL(MAX(b.bid_per_visit),0)的使用解决了您的问题,如果MAX(b.bid_per_visit)为null,则返回0,如果b.bid_per_visit不为null,则返回最大值。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.