Note: This is not a duplicate of: mySQL - Create a New Table Using Data and Columns from Three Tables. That question is about taking data from multiple tables and actually creating a new table with that data. I'm just looking to select already existing data. The answer on that question is very specific to the question that was asked, and doesn't really help me since the code in the answer is not explained. So no, this is not "the exact same question" at all.
I have a question about joining two MySQL tables that I cannot seem to find an answer to. The usual types of joins do not seem to be what I'm after. perhaps this is not possible, I'm not sure.
So I have my first table like this (tables simplified for example purposes):
ID Name Email Phone
1 John john@example.com 000-123-4567
2 Jane jane@example.com 000-890-1234
My second table is arranged this way:
ID UserID meta_key meta_value
1 1 location_review 4
2 1 condition_review 1
3 1 price_review 5
4 2 location_review 4
5 2 condition_review 2
6 2 price_review 4
I am working with some existing software, just making some modifications, so ideally I can do what I need without needing to change how the software is written too much.
I am trying to be able to join these tables so they look like this:
ID Name Email Phone location_review condition_review price_review
1 John john@example.com 000-123-4567 4 1 5
2 Jane jane@example.com 000-890-1234 4 2 4
So I need to add the rows from Table2 that have the UserID to the data already in Table1.
Since this is existing software there is already some code in place to join these two tables, it is here:
SELECT *
FROM wpnf_comments as comment
INNER JOIN wpnf_commentmeta AS meta
WHERE comment.comment_post_ID = $prop_ID
AND meta.meta_key = 'rating'
AND meta.comment_id = comment.comment_ID
AND ( comment.comment_approved = 1
OR comment.user_id = $userID
)
What I am doing is: There used to be only a single "rating" value, stored in the meta table. You can see it in the above Join where it says:
AND meta.meta_key = 'rating'
However, I am trying to replace that with 3 separate ratings. So the join above, that used to work when there was only a single rating, doesn't really work when I have multiple ratings.
I would very much appreciate any insight into what I need to do to fix this. I am also using PHP for this, if that matters.
EDIT
This is the code I have so far. it's showing all of the correct column names I need now, but the actual values/data is not showing up. It's returning 0 rows.
SELECT
*
FROM
wpnf_comments t1
INNER JOIN
(SELECT
comment_id as comment_id2,
MAX(IF(meta_key = 'location-rating', meta_value, NULL)) AS location_rating,
MAX(IF(meta_key = 'condition-rating', meta_value, NULL)) AS condition_rating,
MAX(IF(meta_key = 'maintenance-rating', meta_value, NULL)) AS maintenance_rating,
MAX(IF(meta_key = 'professionalism-rating', meta_value, NULL)) AS professionalism_rating,
MAX(IF(meta_key = 'contact-rating', meta_value, NULL)) AS contact_rating,
MAX(IF(meta_key = 'availability-rating', meta_value, NULL)) AS availability_rating,
MAX(IF(meta_key = 'responsiveness-rating', meta_value, NULL)) AS responsiveness_rating,
MAX(IF(meta_key = 'price-rating', meta_value, NULL)) AS price_rating
FROM
wpnf_commentmeta
GROUP BY comment_id) temp_t
ON t1.comment_post_ID = temp_t.comment_id2