The Little Guy Posted December 4, 2010 Share Posted December 4, 2010 I have 3 tables I need to join together: - contestants - votes - vs the table contestants has two fields that I need: - cont1 - cont2 the table votes has a count of all the votes that I want to do the grouping with and table vs has the title where id = cont1 or id = cont2 So... Here is what I would like to return: - total - vsid - cont1 title - cont2 title Here is what I have SELECT COUNT(*) as total, c1.vid, vs1.vsid, c1.title as title1, c2.title as title2 FROM contestants c1 JOIN vs vs1 ON(c1.id = vs1.cont1) JOIN contestants c2 JOIN vs vs2 ON(c2.id = vs2.cont2) GROUP BY vs1.vsid LIMIT 100 Quote Link to comment https://forums.phpfreaks.com/topic/220627-join-3-tables-4-times/ Share on other sites More sharing options...
The Little Guy Posted December 4, 2010 Author Share Posted December 4, 2010 Okay, the follow query works other than the total column: SELECT COUNT(*) as total, vs.vsid, c1.title as title1, c2.title as title2 FROM vs LEFT JOIN contestants c1 ON(vs.cont1 = c1.id) LEFT JOIN contestants c2 ON(vs.cont2 = c2.id) GROUP BY vs.id Somehow I need to add the table votes in there, and it should total the vsid column. I should get 3 rows back, with the totals column as follows: 5, 1, 2 right now they are all 1's Quote Link to comment https://forums.phpfreaks.com/topic/220627-join-3-tables-4-times/#findComment-1142826 Share on other sites More sharing options...
fenway Posted December 5, 2010 Share Posted December 5, 2010 You can't use GROUP BY and those title fields together. Quote Link to comment https://forums.phpfreaks.com/topic/220627-join-3-tables-4-times/#findComment-1143295 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.