Monday 15 June 2015

left join - MySQL - SUM/COUNT Different Columns From Different Tables -


I have a strange problem, looking at three tables. PRODUCT - ID - title product_rating - product_id - rating product_view - product_id

I want to capture products with a SM of my ratings (which is a +1 or -1 value ) And their total product count.

  SELECT p.`id`, p.`title`, SUM (PR.Rating)) as AS rating, COUNT (PVT Product_ID`) as `product` As left to go as P 'product_reating' on PR (PR.Product_ID = P.IDID) LEFT JOIN as product_view` PV On (PVA product_id '= P.ID') group, p. I have 1 rating, 9 views in the DESC   

by IDID rating. However, the query is returning 9 ratings, 9 views. I understand why this is happening (this is the abbreviation of the rating for each product_view), but I do not know about it.

Any suggestions will be highly appreciated.


Sample Data: Products ------------ ID | Title 1 Globber product_activity ------------------- product_id | Rating 1 | 1 product_view ------------ Product_head 1 1 1 1 1 1 1 1 1 1

Try

  SELECT p.id, p.title, r.rating, v.views Products by product LE LEFT JOIN (SELECT product_id, SUM (rating) Rating Product_AN GROUP BY Product_id) R. P.ID = R.product_id (SELECT product_id, COUNT (*) by product_view GROUP product_id) P on P.ID = v.product_id ORDER R. RATING DESC   

sample output:

 | ID | TITLE | Ratings | VIEWS | --------------------------------- | 1 | Globe | 1 | 9 |  

Here is the demo

No comments:

Post a Comment