Jump to content

table joins


doddsey_65

Recommended Posts

im trying to improve my code by using table joins but when i use the following code it just returns everything in the database rather than the results that equal $forum_id which is the get value 2. There should only be 2 results.

 

 $query = $db->query("SELECT 
                    ".DB_PREFIX."topics.topic_id,
                    ".DB_PREFIX."topics.topic_name,
                    ".DB_PREFIX."topics.topic_poster,
                    ".DB_PREFIX."topics.topic_time_posted,
                    ".DB_PREFIX."topics.topic_views,
                    ".DB_PREFIX."topics.topic_replies,
                    ".DB_PREFIX."topics.topic_last_poster,
                    ".DB_PREFIX."topics.topic_last_post_time,
                    ".DB_PREFIX."topics.topic_locked,
                    ".DB_PREFIX."topics.topic_sticky,
                    
                    ".DB_PREFIX."parents.parent_id,
                    ".DB_PREFIX."parents.parent_name,
                    
                    ".DB_PREFIX."forums.forum_name,
                    
                    ".DB_PREFIX."members.user_username,
                    ".DB_PREFIX."members.user_group
                    
                    FROM ".DB_PREFIX."topics
                    
                     JOIN ".DB_PREFIX."members
                     JOIN ".DB_PREFIX."parents
                     JOIN ".DB_PREFIX."forums
                    
                    ON ".DB_PREFIX."topics.topic_poster 
                    = ".DB_PREFIX."members.user_username
                    
                    WHERE ".DB_PREFIX."forums.forum_id
                    = ".$forum_id."
                    
                    ORDER BY ".DB_PREFIX."topics.topic_time_posted $max")
                    or trigger_error("SQL", E_USER_ERROR);

Link to comment
https://forums.phpfreaks.com/topic/214090-table-joins/
Share on other sites

because your select is doing a couple of JOINS without conditions ... this part has an incorrect format that cause the behavior that you are seeing

 

 

                    FROM ".DB_PREFIX."topics
                     JOIN ".DB_PREFIX."members
                     JOIN ".DB_PREFIX."parents
                     JOIN ".DB_PREFIX."forums
                    ON ".DB_PREFIX."topics.topic_poster 
                    = ".DB_PREFIX."members.user_username

 

it should be something like this (adjust to your table descriptions)

                    FROM ".DB_PREFIX."topics
                     JOIN ".DB_PREFIX."members 
                               ON ".DB_PREFIX."topics.topic_poster  = ".DB_PREFIX."members.user_username
                     JOIN ".DB_PREFIX."parents  ON ".DB_PREFIX."<complete here> = ".DB_PREFIX."<complete here>
                     JOIN ".DB_PREFIX."forums   ON ".DB_PREFIX."<complete here> = ".DB_PREFIX."<complete here>

                   

                   

Link to comment
https://forums.phpfreaks.com/topic/214090-table-joins/#findComment-1114095
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.