KingOfHeart Posted January 6, 2009 Share Posted January 6, 2009 I have a site that contains a file list . I also have a wiki that contains a file list. I want to list both of these files into one group. From Wiki image - Table img_name - Contains the name of the file img_description - Contains the description img_user - Returns the number of the user who uploaded it user - Table user_id - The id for the user user_name - Contains the name of the user From my File List Downloads - Table name - Name of the file userid - The user's id description - The description for the file type - The file's Type Accounts - Table id - The user's ID username - The user's name So first we need to get it organized for the image table to be combined with the user table. Then Downloads needs to be combined with Accounts. After all that is done, the two lists need to come together. So how tough would this be? I'd settle for keeping the Wiki data and the File List separated into two lists if necessary. Quote Link to comment https://forums.phpfreaks.com/topic/139701-taking-data-from-two-tables/ Share on other sites More sharing options...
fenway Posted January 7, 2009 Share Posted January 7, 2009 It's not tough at all.. a few joins ought to do the trick. What do you want to display at the edn? Quote Link to comment https://forums.phpfreaks.com/topic/139701-taking-data-from-two-tables/#findComment-731427 Share on other sites More sharing options...
KingOfHeart Posted January 7, 2009 Author Share Posted January 7, 2009 img_name, user_name, img_description, name, username, description would need to be echoed. The other fields will be needed though for getting certain data. Haven't had much practice with the JOIN command. Quote Link to comment https://forums.phpfreaks.com/topic/139701-taking-data-from-two-tables/#findComment-732071 Share on other sites More sharing options...
fenway Posted January 8, 2009 Share Posted January 8, 2009 Try this: ( select i.img_name , i.img_description , u.user_name from image as i inner join user at t on ( t.user_id = i.img_user ) ) union all ( select d.name , d.description , a.username from downloads as d inner join accounts as a on ( a.id = d.userid ) ) Quote Link to comment https://forums.phpfreaks.com/topic/139701-taking-data-from-two-tables/#findComment-732266 Share on other sites More sharing options...
KingOfHeart Posted January 8, 2009 Author Share Posted January 8, 2009 I'll try it out later but I have a question first. When echoing the rows can I use $row['d.description'] at all? Or is this a wrong method to use? Quote Link to comment https://forums.phpfreaks.com/topic/139701-taking-data-from-two-tables/#findComment-732427 Share on other sites More sharing options...
fenway Posted January 8, 2009 Share Posted January 8, 2009 The table prefix is not part of the field name that you will retrieve... in fact, it will probably have the name of the first half of the query. Quote Link to comment https://forums.phpfreaks.com/topic/139701-taking-data-from-two-tables/#findComment-732510 Share on other sites More sharing options...
KingOfHeart Posted January 8, 2009 Author Share Posted January 8, 2009 How do I set this up? $sql = mysql_query("( select i.img_name , i.img_description , u.user_name from image as i inner join user at t on ( t.user_id = i.img_user ) ) union all ( select d.name , d.description , a.username from downloads as d inner join accounts as a on ( a.id = d.userid ) ) "); while($rows= mysql_fetch_array($sql)) { $out .= $rows['name']; } $out is for just returning the data. Quote Link to comment https://forums.phpfreaks.com/topic/139701-taking-data-from-two-tables/#findComment-732943 Share on other sites More sharing options...
fenway Posted January 9, 2009 Share Posted January 9, 2009 How do I set this up? Easiest way is simply to use the same column aliases for both parts of the union. Quote Link to comment https://forums.phpfreaks.com/topic/139701-taking-data-from-two-tables/#findComment-733113 Share on other sites More sharing options...
KingOfHeart Posted January 9, 2009 Author Share Posted January 9, 2009 Can you just paste the entire coded needed, showing the mysql line and $rows This is the part that throws me off. The last code I used just created an error. Quote Link to comment https://forums.phpfreaks.com/topic/139701-taking-data-from-two-tables/#findComment-733320 Share on other sites More sharing options...
Kouruu Posted January 10, 2009 Share Posted January 10, 2009 fixed some errors in it, and got it decent $sql = mysql_query("( select i.img_name , i.img_description , t.user_name from image as i inner join user as t on ( t.user_id = i.img_user ) ) union all ( select d.name , d.description , a.username from Downloads as d inner join Accounts as a on ( a.id = d.userid ) ) "); mind you, due to the fact that the fields of the first group and second group don't match, you can't use a union here, the only way would be if they each had the same fields. Quote Link to comment https://forums.phpfreaks.com/topic/139701-taking-data-from-two-tables/#findComment-733837 Share on other sites More sharing options...
fenway Posted January 11, 2009 Share Posted January 11, 2009 mind you, due to the fact that the fields of the first group and second group don't match, you can't use a union here, the only way would be if they each had the same fields. Not true at all... like I said earlier, use column aliases to make them the same. Quote Link to comment https://forums.phpfreaks.com/topic/139701-taking-data-from-two-tables/#findComment-734569 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.