1

I wonder if anyone could help me.

I have 4 tables that need to be joined to retrieve the data i need.

reviews

themes

reviewthemes

reviewsubthemes

So i want to do a simple select * from reviews. With this i require the theme name from themes and the subtheme name from themes.

A review can have more than one theme and subtheme. Also a review may only have a theme with no subthemes and then another theme with subthemes.

themes table look like this

enter image description here

Both reviewthemes and reviewsubthemes look like this

enter image description here

reviews table looks like this:

enter image description here

I have tried but i am getting too confused.

So essentially the review table holds the reviews. The themes table is where the name of the themes are, and both reviewthemes and reviewsubthemes have a reviewID column to join with the reviews table and a themeID to joni to the themes table.

Does anyone know how to do this and also can you explain how the query is made.

Thanks

2
  • 1
    What does the reviews table look like?
    – Kalessin
    Sep 10, 2015 at 14:48
  • @kalessin i have edited my question with the review table image Sep 10, 2015 at 14:50

2 Answers 2

1

You may need to do a left join using reviews as your left table and then match themes based on themeid from either one of the tables: reviewthemes or reviewsubthemes.

select r.*,
       t.name
from reviews r
left join reviewthemes rt on rt.reviewId = r.reviewId
left join reviewsubthemes rst on rst.reviewid = r.reviewid
left join themes t on (t.themeid = rt.themeid or t.themeid = rst.themeid)

You could also use UNION or UNION ALL to do this (preferable method):

select r.*,
       t.name
from reviews r
left join reviewthemes rt on rt.reviewId = r.reviewId
left join themes t on t.themeid = rt.themeid 

union all

select r.*,
       t.name
from reviews r
left join reviewsubthemes rst on rst.reviewid = r.reviewid
left join themes t on t.themeid = rst.themeid

UPDATE:

Based on your comments, here is the answer:

select r.*,
       GROUP_CONCAT(t.name) as Name
from reviews r
left join reviewthemes rt on rt.reviewId = r.reviewId
left join reviewsubthemes rst on rst.reviewid = r.reviewid
left join themes t on (t.themeid = rt.themeid or t.themeid = rst.themeid)
group by r.*
10
  • I tried this, it does work but not exactly how i wanted it too. This only puts 1 theme on each row. So it could have the same review 5 times all exactly the same except for the theme name. Instead if a review has multiple themes or sub themes i need these to be on the same row in different columns. is this possible? Sep 10, 2015 at 15:07
  • Look into Group_Concat if you want to put other themes/subthemes on the same row. Without sample data and expected output, it would be difficult to envision what you are trying to achieve.
    – FutbolFan
    Sep 10, 2015 at 15:10
  • well at the minute i have 5 rows with the same review. Everything is the same except for the theme column. Because there are 5 themes that link to this particular review it has made 5 different rows Sep 10, 2015 at 15:13
  • 1
    aha i'm sorry, i must of not been paying attention to where i was clicking. I have changed it now :) Sep 10, 2015 at 15:49
  • 1
    aha that would have been a little sad , someone helps and goes through the question, solves your problem.... then you vote someone else lol Sep 10, 2015 at 15:50
1

Maybe I don't have the full picture but you might have too many tables, a themeID field in the Review table plus a theme/subtheme flag could have been enough. Anyway, try this:

select r.*, t.name
  from reviews as r
  left join reviewthemes as rt on r.themeID = rt.themeID
  left join reviewsubthemes as rst on r.themeID = rst.themeID
  left join themes as t on r.themeID = t.themeID

This should work if it's the themeID field has the ID you want and not the ID field. Add more fields to the select as you wish.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.