SELECT
games.id AS id,
games.ean AS ean,
games.title AS title,
games.developer AS developer,
reviews.NoReviews AS NoReviews
FROM
games
INNER JOIN
(SELECT
ean,
COUNT(*) AS NoReviews
FROM
reviews
GROUP BY ean) reviews
ON games.ean = reviews.ean
WHERE games.genre = genre
我遇到上述代码的问题,只有当游戏有评论时才会返回数据,但是当游戏没有我想要的评论时没有返回任何内容
例如,如果游戏没有评论,它仍然应该从该类型返回游戏,但是如果没有评论,则计数为0。当我尝试更改任何东西时,即使有匹配也不会发回任何数据。所以我把它还原回到我上面的工作方法。
答案 0 :(得分:0)
尝试将Inner join
更改为left join
,内部联接仅为您提供匹配记录,而左连接将为您提供左表中的所有行,即使找不到匹配项
SELECT
games.id AS id,
games.ean AS ean,
games.title AS title,
games.developer AS developer,
reviews.NoReviews AS NoReviews
FROM
games
LEFT JOIN
(SELECT
ean,
COUNT(*) AS NoReviews
FROM
reviews
GROUP BY ean) reviews
ON games.ean = reviews.ean
WHERE games.genre = genre
答案 1 :(得分:0)
将INNER JOIN
更改为LEFT OUTER JOIN
查询: -
SELECT games.id AS id,games.ean AS ean, games.title AS title, games.developer AS developer, reviews.NoReviews AS NoReviews
FROM games left outer join
(SELECT ean, COUNT(*) as NoReviews
FROM reviews GROUP BY ean) reviews
ON games.ean = reviews.ean
WHERE games.genre=genre
左外连接将显示左表中的所有记录,以及右表中的匹配记录。