stubarny Posted November 17, 2011 Share Posted November 17, 2011 Hello, This code below selects all the records in table "research_job_searches" that have corresponding records in table "research_job_searches_history". $query = "SELECT * FROM research_job_searches INNER JOIN research_job_searches_history ON research_job_searches.research_job_search_index=research_job_searches_history.research_job_search_history_index"; Please could you tell me how to re-write this code so that it selects all the records in table "research_job_searches" that do not have corresponding records in table "research_job_searches_history"? Thanks, Stu Quote Link to comment https://forums.phpfreaks.com/topic/251280-inner-join-statement/ Share on other sites More sharing options...
Zane Posted November 17, 2011 Share Posted November 17, 2011 You would use a LEFT OUTER JOIN for this, with a WHERE clause. Also, it wouldn't hurt to assign aliases for easier coding. SELECT * FROM research_job_searches a LEFT OUTER JOIN research_job_searches_history b ON a.research_job_search_index=b.research_job_search_history_index WHERE b.research_job_search_history_index IS NULL Here's a great tutorial on JOINs, with pictures! http://www.codinghorror.com/blog/2007/10/a-visual-explanation-of-sql-joins.html Quote Link to comment https://forums.phpfreaks.com/topic/251280-inner-join-statement/#findComment-1288823 Share on other sites More sharing options...
stubarny Posted November 17, 2011 Author Share Posted November 17, 2011 Wow, thanks Zane that link cleared up 2 years of confusion! How would I limit this to return no more than one (joined) record? Quote Link to comment https://forums.phpfreaks.com/topic/251280-inner-join-statement/#findComment-1289143 Share on other sites More sharing options...
fenway Posted November 18, 2011 Share Posted November 18, 2011 How would I limit this to return no more than one (joined) record? That doesn't exist. Set theory doesn't allow for "one of".Which one would you want? Based on that, you can use a self-join to limit that result to a single record, and then you're golden. Quote Link to comment https://forums.phpfreaks.com/topic/251280-inner-join-statement/#findComment-1289180 Share on other sites More sharing options...
stubarny Posted November 19, 2011 Author Share Posted November 19, 2011 Hi fenway, sorry I'm lost - I thought a self-join statement is used on two copies of the same table? how would I apply a self-join statement to Zane's code? I just need 1 matching record to be returned (it doesn't matter which matching record is returned) Thanks, Stu Quote Link to comment https://forums.phpfreaks.com/topic/251280-inner-join-statement/#findComment-1289567 Share on other sites More sharing options...
fenway Posted November 19, 2011 Share Posted November 19, 2011 You'd use a self-join to find the record you want -- then join the result back to whatever other tables(s) you need. Quote Link to comment https://forums.phpfreaks.com/topic/251280-inner-join-statement/#findComment-1289602 Share on other sites More sharing options...
stubarny Posted November 19, 2011 Author Share Posted November 19, 2011 Hi Fenway, I would agree with you if I was trying to return a record from research_job_searches_history, but I'm trying to return a record that does not exist in research_job_searches_history. Therefore surely I can't perform a select operation without referring to the other table (research_job_searches_history)? Stu Quote Link to comment https://forums.phpfreaks.com/topic/251280-inner-join-statement/#findComment-1289606 Share on other sites More sharing options...
fenway Posted November 20, 2011 Share Posted November 20, 2011 Sorry, I missed that somehow -- then you want " <> ANY "> Quote Link to comment https://forums.phpfreaks.com/topic/251280-inner-join-statement/#findComment-1289738 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.