socalnate Posted June 29, 2007 Share Posted June 29, 2007 Here's the statement: SELECT gamesnew.game_name, sort_date, thumb, flash_file, genres.genre, descriptions.description, ratings.num_votes, score FROM gamesnew LEFT JOIN genres ON gamesnew.game_name = genres.game_name JOIN descriptions ON genres.game_name = descriptions.game_name JOIN ratings ON descriptions.game_name = ratings.game_name Each table has one column [game_name] that is unique per table (ie the game names aren't duplicated). I set the KEY as the ID (which varies per table so it's useless for my purposes) but my game_name in each table I set to UNIQUE so that for each table the unique identifier is the game name and nothing else. The SQL statement works but the problem is is that it doesn't select ALL the [game_name]'s from the table gamesnew even if the info isn't complete for the other tables. It should (in theory) select all 18,000 game_name and display them with the joined content from the other tables and just leave blanks in the incomplete info right? I don't know why it's only selecting about 1,500 games (but oddly all the info is complete with the data it does select). ??? Quote Link to comment https://forums.phpfreaks.com/topic/57676-complex-sql-join-statement-sorta-worksneed-help/ Share on other sites More sharing options...
bubblegum.anarchy Posted June 29, 2007 Share Posted June 29, 2007 All the joins need to be LEFT JOINS Quote Link to comment https://forums.phpfreaks.com/topic/57676-complex-sql-join-statement-sorta-worksneed-help/#findComment-285571 Share on other sites More sharing options...
socalnate Posted June 30, 2007 Author Share Posted June 30, 2007 Thanks, your suggestion worked (partially). Here's the SQL statement I used: SELECT gamesnew.game_name, sort_date, thumb, flash_file, genres.genre, descriptions.description, ratings.num_votes, score FROM gamesnew LEFT JOIN genres ON gamesnew.game_name = genres.game_name LEFT JOIN descriptions ON genres.game_name = descriptions.game_name LEFT JOIN ratings ON descriptions.game_name = ratings.game_name Now I have the problem where in my ratings table it isn't selecting ALL of the columns [num_votes] or [score] even though in those columns where the data is missing the game_name is exactly the same. How come the join is partially working? It's weird, I did some sampling of the game_name to make sure they were exactly the same and they were. Any ideas? ??? Quote Link to comment https://forums.phpfreaks.com/topic/57676-complex-sql-join-statement-sorta-worksneed-help/#findComment-286316 Share on other sites More sharing options...
rcorlew Posted June 30, 2007 Share Posted June 30, 2007 left joins only returns relevant data(only table.rows) that are not null right joins return all data, whether it is null or not. If you want to return rows that have null rows, you should try using right join Quote Link to comment https://forums.phpfreaks.com/topic/57676-complex-sql-join-statement-sorta-worksneed-help/#findComment-286785 Share on other sites More sharing options...
fenway Posted July 1, 2007 Share Posted July 1, 2007 left joins only returns relevant data(only table.rows) that are not null right joins return all data, whether it is null or not. If you want to return rows that have null rows, you should try using right join No... never use right join, just flip the tables around. Quote Link to comment https://forums.phpfreaks.com/topic/57676-complex-sql-join-statement-sorta-worksneed-help/#findComment-287040 Share on other sites More sharing options...
rcorlew Posted July 1, 2007 Share Posted July 1, 2007 Then they would need to an OUTER JOIN, INNER JOINS will only return non-empty rows, it appears that they would like to select all rows based on the post even if data is missing from any of the rows for num_votes and scores. Quote Link to comment https://forums.phpfreaks.com/topic/57676-complex-sql-join-statement-sorta-worksneed-help/#findComment-287044 Share on other sites More sharing options...
fenway Posted July 10, 2007 Share Posted July 10, 2007 Yes, and a LEFT JOIN is an outer join that's standard. Quote Link to comment https://forums.phpfreaks.com/topic/57676-complex-sql-join-statement-sorta-worksneed-help/#findComment-294599 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.