You can also add if condition inside of your mysql query like below.
SELECT 
   rating,
   trade_service_id,
   ifnull(round(sum(belonging_product_ratings.rating)/count(*),0),0) AS avg,
   count(*) AS count
FROM
   belonging_product_ratings WHERE trade_service_id != 0
GROUP BY
   belonging_product_ratings.trade_service_id
other alternative is as below
select
   BelongingProductRating.*,
   User.*
from
   belonging_product_ratings AS BelongingProductRating
LEFT OUTER JOIN
   users AS User ON
   IF( BelongingProductRating.rated_user_id != '$userID', BelongingProductRating.rated_user_id, BelongingProductRating.user_id) = User.id
You can say if condition inside mysql query is same as ternary if.
You can set query according to your php variable too.
I am sharing this code which i was ended up.
all you need to do is set it according to your table fields and conditions.
feel free to ask.