stubarny Posted November 25, 2011 Share Posted November 25, 2011 Hello, I have a query that finds details of job searches made on my (recruitment) site. It currently returns all matches on "a.research_job_search_index=b.research_job_search_history_research_job_search_index" that are more than 24hrs old. Please could you tell me how to change this query so that it returns only the most recent match for each set of matches of "a.research_job_search_index=b.research_job_search_history_research_job_search_index" that are more than 24hrs old? $query = "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_research_job_search_index WHERE b.research_job_search_history_timestamp < $timestamp_1_day_ago"; Many thanks for your help, Stu Quote Link to comment https://forums.phpfreaks.com/topic/251804-finding-the-greatest-match/ Share on other sites More sharing options...
fenway Posted November 26, 2011 Share Posted November 26, 2011 See here. Quote Link to comment https://forums.phpfreaks.com/topic/251804-finding-the-greatest-match/#findComment-1291312 Share on other sites More sharing options...
stubarny Posted November 26, 2011 Author Share Posted November 26, 2011 Hi fenway, Many thanks. I'm having some trouble combining your previous solution (a self-join) with my query. Here's where I've got to: $query = "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_research_job_search_index AND a.research_job_search_history_timestamp < b.research_job_search_history_timestamp ) WHERE b.research_job_search_history_timestamp is null"; The problem is that a.research_job_search_history_timestamp is not a field in my table a. Please could you show me how to integrate your self-join query within my query? Many thanks, Stu Quote Link to comment https://forums.phpfreaks.com/topic/251804-finding-the-greatest-match/#findComment-1291327 Share on other sites More sharing options...
stubarny Posted November 27, 2011 Author Share Posted November 27, 2011 Hi fenway, Thanks for your help - I appear to have fixed it but have just 1 more question Instead of selecting all columns using "*", I'd like to select all columns from table alias b - please could you tell me if this is possible? $query = "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_research_job_search_index AND b.research_job_search_history_timestamp < $timestamp_1_day_ago ) LEFT OUTER JOIN research_job_searches_history c ON ( (a.research_job_search_index = c.research_job_search_history_research_job_search_index) AND b.research_job_search_history_timestamp < c.research_job_search_history_timestamp ) WHERE c.research_job_search_history_timestamp IS NULL"; Many thanks for your help - you saved me a lot of time today! Stu Quote Link to comment https://forums.phpfreaks.com/topic/251804-finding-the-greatest-match/#findComment-1291438 Share on other sites More sharing options...
fenway Posted November 27, 2011 Share Posted November 27, 2011 So, b.*? Quote Link to comment https://forums.phpfreaks.com/topic/251804-finding-the-greatest-match/#findComment-1291473 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.