2009-09-05 63 views
0
SELECT videos.id, videos.game_id, videos.xbox360, videos.ps3, videos.pc, 
    videos.wii, videos.other, videos.thumbnail, videos.vid_info, videos.sdvid, 
    videos.hdvid, UNIX_TIMESTAMP(videos.date_added) , game_data.name, 
    AVG(video_ratings.rating) 
FROM videos, game_data, video_ratings 
WHERE videos.game_id = game_data.id 
    AND videos.id = video_ratings.video_id 
GROUP BY videos.id, video_ratings.video_id 
ORDER BY videos.date_added DESC LIMIT 10; 

我運行此查詢從三個表video_ratingsgame_data,視頻中提取數據...如何在外鍵不返回任何內容時在MySQL中選擇記錄?

現在我面臨的問題是,結果只顯示已被評爲(或者是在視頻表video_ratings),因爲AND videos.id = video_ratings.video_id ...

有什麼辦法,我可以選擇所有的視頻數據,結果表明AVG(video_ratings.rating)爲空,如果爲這些影片的收視率不存在video_ratings表(或者說沒有的視頻已被評分,因此結果必須顯示10行AVG(video_ratings.rating)列空)...

感謝

回答

3

呀,只是用左外內而不是內部連接(這是你在做什麼)。我也建議你改用JOIN語法。它更清晰:

SELECT v.id, v.game_id, v.xbox360, v.ps3, v.pc, v.wii, 
v.other, v.thumbnail, v.vid_info, v.sdvid, v.hdvid, 
UNIX_TIMESTAMP(v.date_added), gd.name, AVG(vg.rating) 
FROM videos v 
LEFT JOIN game_data gd ON gd.id = v.game_id 
LEFT JOIN video_ratings vr ON v.id = vr.video_id 
GROUP BY videos.id, video_ratings.video_id 
ORDER BY videos.date_added DESC LIMIT 10 
+0

這是顯示一個愚蠢的錯誤..我不知道爲什麼??#1054 - 未知列'videos.id'在'字段列表'....我的查詢運行完全正常你的也似乎合法! – halocursed 2009-09-05 09:03:07

+0

謝謝你克萊斯先生 – halocursed 2009-09-05 09:12:03

相關問題