I am trying to join 2 tables and grab a single row for each item. The default value for the rating is 45. Every time there is a different rating within a category, each item row is duplicated. Below is an example schema, data, and result.
Table 1
--------------------------
ofid | item | category
--------------------------
1 | item1 | cat1
2 | item2 | cat1
3 | item3 | cat1
Table 2
---------------------------
ofid | rating | category
---------------------------
1 | 37 | cat1
2 | 42 | cat1
3 | 45 | cat1
Result
------------------------------------
ofid | item | rating | category
------------------------------------
1 | item1 | 37 | cat1
1 | item1 | 42 | cat1
1 | item1 | 45 | cat1
2 | item2 | 37 | cat1
2 | item2 | 42 | cat1
2 | item2 | 45 | cat1
3 | item3 | 37 | cat1
3 | item3 | 42 | cat1
3 | item3 | 45 | cat1
Expected Result
------------------------------------
ofid | item | rating
------------------------------------
1 | item1 | 37
2 | item2 | 42
3 | item3 | 45
Query:SELECT p.ofid,p.item,r.rating FROM table1 AS p INNER JOIN table2 AS r ON p.category = r.category WHERE p.category = "cat1" GROUP BY p.ofid,p.category,r.rating
I've tried adding DISTINCT, JOIN, LEFT JOIN, RIGHT JOIN.... help?
Thanks in advance.