合并多个表并使用 COALESCE 的奇怪行为
Odd behavior combining multiple tables and using COALESCE
我有一个很大的问题,我一直在努力解决并调整了一段时间。
SELECT
tastingNotes.userID, tastingNotes.beerID, tastingNotes.noteID,
tastingNotes.note, user.userName,
COALESCE(sum(tasteNoteRate.score),0) as `score`
FROM
tastingNotes
INNER JOIN `user` on tastingNotes.userID = `user`.userID
LEFT JOIN tasteNoteRate on tastingNotes.noteID = tasteNoteRate.noteID
WHERE tastingNotes.beerID = 'C5RJc0'
GROUP BY tastingNotes.noteID
ORDER BY score DESC
LIMIT 0,50;
我正在使用 COALESCE(sum(tasteNoteRate.score),0)
给结果返回零值,如果他们还没有分数的话。
奇怪的是,当我应该有两个结果时,它只返回一个分数为零的音符。
然后当我给一个分数时,他们都出现了,一个是它的分数,然后是第二个是零分。
尝试
SELECT q.noteID, q.userID, q.beerID, q.note, q.score, u.userName
FROM (
SELECT n.noteID, n.userID, n.beerID, n.note, COALESCE(SUM(r.score), 0) score
FROM tastingNotes n LEFT JOIN tasteNoteRate r
ON n.noteID = r.noteID
WHERE n.beerID = 'C5RJc0'
GROUP BY n.noteID, n.userID, n.beerID, n.note
) q JOIN `user` u ON q.userID = u.userID
ORDER BY score DESC
LIMIT 50
我有一个很大的问题,我一直在努力解决并调整了一段时间。
SELECT
tastingNotes.userID, tastingNotes.beerID, tastingNotes.noteID,
tastingNotes.note, user.userName,
COALESCE(sum(tasteNoteRate.score),0) as `score`
FROM
tastingNotes
INNER JOIN `user` on tastingNotes.userID = `user`.userID
LEFT JOIN tasteNoteRate on tastingNotes.noteID = tasteNoteRate.noteID
WHERE tastingNotes.beerID = 'C5RJc0'
GROUP BY tastingNotes.noteID
ORDER BY score DESC
LIMIT 0,50;
我正在使用 COALESCE(sum(tasteNoteRate.score),0)
给结果返回零值,如果他们还没有分数的话。
奇怪的是,当我应该有两个结果时,它只返回一个分数为零的音符。
然后当我给一个分数时,他们都出现了,一个是它的分数,然后是第二个是零分。
尝试
SELECT q.noteID, q.userID, q.beerID, q.note, q.score, u.userName
FROM (
SELECT n.noteID, n.userID, n.beerID, n.note, COALESCE(SUM(r.score), 0) score
FROM tastingNotes n LEFT JOIN tasteNoteRate r
ON n.noteID = r.noteID
WHERE n.beerID = 'C5RJc0'
GROUP BY n.noteID, n.userID, n.beerID, n.note
) q JOIN `user` u ON q.userID = u.userID
ORDER BY score DESC
LIMIT 50