mofolo Posted April 18, 2010 Share Posted April 18, 2010 Hey Guys, I need to intersect a table using the values from a query.. For Example: $myData = mysql_query("SELECT theID FROM myData WHERE myID = '$userID'"); Now I need to use the Multiple Results returned into the Query $myData to look for data in another table. For Example: mysql_query("SELECT * FROM otherTable WHERE personID = '$myData'") (that Query Will not work...) So what I am trying to do is return the list of 'theID' from the table 'myData'. Then with the list of IDs, get all the related rows from 'otherTable'. Whats the most effective and efficient way of getting it using mySQL? Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/198926-using-a-query-to-intersect-another-table/ Share on other sites More sharing options...
fenway Posted April 18, 2010 Share Posted April 18, 2010 It's called a JOIN. Quote Link to comment https://forums.phpfreaks.com/topic/198926-using-a-query-to-intersect-another-table/#findComment-1044199 Share on other sites More sharing options...
mofolo Posted April 18, 2010 Author Share Posted April 18, 2010 I've look at some Tutorials about Join, but they're confusing. I tried this with no luck. "SELECT theID FROM myData JOIN otherTable ON myData.id = otherTable.id WHERE personID = '$id'" But it only Returns and Joins the first row. Quote Link to comment https://forums.phpfreaks.com/topic/198926-using-a-query-to-intersect-another-table/#findComment-1044345 Share on other sites More sharing options...
fenway Posted April 19, 2010 Share Posted April 19, 2010 I'm not sure I follow -- (a) you're only asking for "theID" -- don't know what it is -- and (b) check how many matching records actually exist. Quote Link to comment https://forums.phpfreaks.com/topic/198926-using-a-query-to-intersect-another-table/#findComment-1044632 Share on other sites More sharing options...
aebstract Posted April 19, 2010 Share Posted April 19, 2010 $myData = mysql_query("SELECT theID FROM myData WHERE myID = '$userID'"); mysql_query("SELECT * FROM otherTable WHERE personID = '$myData'") So what I am trying to do is return the list of 'theID' from the table 'myData'. Then with the list of IDs, get all the related rows from 'otherTable'. You're wanting to grab your first result and join your other table based on the id equalling the personID. I think I got what you want right here, not sure if the table-data matches to what you're doing but hopefully $query = mysql_query(" SELECT * FROM myData INNER JOIN otherTable ON myData.myID = personID.otherTable WHERE myID.myData = '$userID' "); Quote Link to comment https://forums.phpfreaks.com/topic/198926-using-a-query-to-intersect-another-table/#findComment-1044638 Share on other sites More sharing options...
mofolo Posted April 19, 2010 Author Share Posted April 19, 2010 That did it! Thanks for the help Quote Link to comment https://forums.phpfreaks.com/topic/198926-using-a-query-to-intersect-another-table/#findComment-1044903 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.