开发者

SQL combine COUNT and AVG query with SELECT

开发者 https://www.devze.com 2023-04-05 07:51 出处:网络
I need to get the average rating and the total number of ratings for a particular user and then select all single ratings (rating_value, rating_text, creator) as well:

I need to get the average rating and the total number of ratings for a particular user and then select all single ratings (rating_value, rating_text, creator) as well:

$开发者_StackOverflow社区rating_query = mysql_query("SELECT COUNT(1) as rating_count 
,AVG(rating_value), rating_value, rating_text, creator
FROM user_rating WHERE rated_user = $user_id");

This query would return the COUNT(1) result and the AVG(rating_value) for every row, but I only need those values once.

Is there any way to do this without making 2 separate queries?


There may be a trick I'm not aware of, but I don't think that's possible to do in a single query. You could try using a GROUP BY clause if that would make sense for you, but I'm guessing it probably doesn't from the column names you're using. Any relation requires a single atomic value at any given row and column, even if that value is null. What you are requesting is that columns 1 and 2 in every row but the first have no value, and again I don't think this is possible.

0

精彩评论

暂无评论...
验证码 换一张
取 消