KevinM1 Posted March 31, 2010 Share Posted March 31, 2010 I have a many-to-many relationship in a game review site I'm working on. A review can be for a game that is available on several platforms (example: the Grand Theft Auto games), and each platform will have many reviews, one for each game reviewed on that platform. So, to represent this relationship, I have a pivot table that holds the id for both reviews and platforms. In other words, my setup is: reviews: int id, varchar title, text content, etc. platforms: int id, varchar name platforms_reviews int id, int platform_id, int review_id To figure out what platforms a particular game review is for, I have the following query: SELECT name FROM platforms WHERE id IN (SELECT platform_id FROM platforms_reviews WHERE review_id = {$review['id']}) I'm just wondering if there's a better way to do it, perhaps with a JOIN. Quote Link to comment https://forums.phpfreaks.com/topic/197176-wondering-if-theres-a-better-way-in-vs-join/ Share on other sites More sharing options...
Zane Posted March 31, 2010 Share Posted March 31, 2010 SELECT platforms.name FROM platforms_reviews LEFT JOIN platforms ON platforms_reviews.platform_id = platforms.id WHERE platforms_reviews.review_id = {$review['id']} Quote Link to comment https://forums.phpfreaks.com/topic/197176-wondering-if-theres-a-better-way-in-vs-join/#findComment-1034983 Share on other sites More sharing options...
KevinM1 Posted March 31, 2010 Author Share Posted March 31, 2010 Thanks. My SQL skills, in a word, suck. Definitely my weakest area. Quote Link to comment https://forums.phpfreaks.com/topic/197176-wondering-if-theres-a-better-way-in-vs-join/#findComment-1034988 Share on other sites More sharing options...
Zane Posted March 31, 2010 Share Posted March 31, 2010 Mine aren't the sharpest either. I'm surprised that query I gave you worked.. I liked your version better though.. I didn't know you could do an IN (SELECT... Quote Link to comment https://forums.phpfreaks.com/topic/197176-wondering-if-theres-a-better-way-in-vs-join/#findComment-1034990 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.