AVG + Inner join

随声附和 提交于 2020-01-16 05:11:34

问题


I'm trying to make something like a chart list and my actual query is like this:

SELECT 
    user.username, 
    songs.*, 
    albums.desc, 
    albums.release, 
    albums.name, 
    AVG(songrating.rating)  
FROM 
    songs 
INNER JOIN 
    user 
ON 
    songs.userid=user.id 
INNER JOIN 
    albums 
ON 
    songs.albumid=albums.id 
INNER JOIN  
    songrating  
ON 
    songs.id=songrating.songid 
GROUP BY 
songrating.songid

it only shows entries with at least one rating entry, but I also want these without a rating

I tried to use it with if / case but it doesn't work (or I'm doing something wrong)

If I remove the avg etc, it works correctly.


回答1:


It may be JOIN issue

The INNER JOIN keyword selects all rows from both tables as long as there is a match between the columns in both tables.

The LEFT JOIN keyword returns all rows from the left table (table1), with the matching rows in the right table (table2). The result is NULL in the right side when there is no match.

Try LEFT JOIN



来源:https://stackoverflow.com/questions/17031556/avg-inner-join

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!