pltm_dev
pltm_dev

Reputation: 47

SQL combine COUNT and AVG query with SELECT

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:

$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?

Upvotes: 0

Views: 1094

Answers (1)

Kiley Naro
Kiley Naro

Reputation: 1769

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.

Upvotes: 1

Related Questions