sciencebear Posted May 4, 2009 Share Posted May 4, 2009 I'm trying to run a query. Basically, the first table is a friends system. It has a column for a user and a column his/her friend. What I want to do is select all of the friends from the second column for whenever the first column is who is logged in. I can do, this, and have no problem with it. However, with these names, I want to use them as the parameters in a different table and select every row where the information in one column matches any of the friends selected from the first table. I have an idea of how this would work, but I am not sure. Does anyone know for sure how to do this? Quote Link to comment https://forums.phpfreaks.com/topic/156787-solved-cross-table-selection/ Share on other sites More sharing options...
Ken2k7 Posted May 4, 2009 Share Posted May 4, 2009 I don't know if this works, but logic wise, it seems right. SELECT t2.* FROM `table_two` t2 WHERE t2.column_name IN ( SELECT t1.friend FROM `table_one` t1 WHERE t1.user = '$user' ); Quote Link to comment https://forums.phpfreaks.com/topic/156787-solved-cross-table-selection/#findComment-825701 Share on other sites More sharing options...
sciencebear Posted May 6, 2009 Author Share Posted May 6, 2009 Nah, it didn't work. Basically, what I really need to know is this: I select an array of all the different friend ids from the first table. I need to use this to select all the rows in the second table where the data from a column matches any of the ids in the array, but I don't know how to do this. Quote Link to comment https://forums.phpfreaks.com/topic/156787-solved-cross-table-selection/#findComment-827453 Share on other sites More sharing options...
Ken2k7 Posted May 6, 2009 Share Posted May 6, 2009 Can you show me what you have right now? By the way, in my query, there is a variable $user. Take the line out if it doesn't apply. Quote Link to comment https://forums.phpfreaks.com/topic/156787-solved-cross-table-selection/#findComment-827467 Share on other sites More sharing options...
sciencebear Posted May 7, 2009 Author Share Posted May 7, 2009 Alright here goes. Table 1-friends col1:userid col2:friendid Table 2-post col1:id col2:userid col3... col4... (The other two columns aren't used for selection, but they need to be selected.) This is basically what I have so far: ($userid is supplied when logged in) $row1 = mysql_query("SELECT friendid FROM friends WHERE userid = '$userid'"); $row2 = mysql_fetch_array($row1); $row3 = mysql_query("SELECT MAX(id) FROM post WHERE userid = '$row2'"); $row4 = mysql_fetch_array($row3); Quote Link to comment https://forums.phpfreaks.com/topic/156787-solved-cross-table-selection/#findComment-828443 Share on other sites More sharing options...
Ken2k7 Posted May 7, 2009 Share Posted May 7, 2009 Did you try my SQL? Can you show me the code using the SQL I wrote? Quote Link to comment https://forums.phpfreaks.com/topic/156787-solved-cross-table-selection/#findComment-828486 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.