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视频...

现在我面临的问题是结果仅显示已评级(或在表格中video_ratings)的视频,因为AND videos.id = video_ratings.video_id......

有什么方法可以选择所有视频的数据,如果表格AVG(video_ratings.rating)中不存在这些视频的评级,结果显示为空video_ratings(或者说没有视频被评级,因此结果必须显示 10 行,AVG(video_ratings.rating)列为空值 ) ...

谢谢

4

1 回答 1

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
于 2009-09-05T08:55:28.200 回答