Jump to content

finding the greatest match


stubarny

Recommended Posts

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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!  :D

 

Stu

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.