Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5995531
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T23:58:22+00:00 2026-05-22T23:58:22+00:00

I need some help with a MySQL query I’m working on. I have data

  • 0

I need some help with a MySQL query I’m working on. I have data as follows.

Table 1

id   date1        text     number
---|------------|--------|-------
1  | 2012-12-12 | hi     | 399 
2  | 2011-11-11 | so     | 399
5  | 2010-10-10 | what   | 555
3  | 2009-09-09 | bye    | 300
4  | 2008-08-08 | you    | 300

Table 2

id   number   date2        ref
---|--------|------------|----
1  | 399    | 2012-06-06 | 40
2  | 399    | 2011-06-06 | 50
5  | 555    | 2011-03-03 | 60

For each row in Table 1, I want to get zero or one ref values from Table 2. There should be a row in the result for each row in Table 1. The number column isn’t unique to either table, so the join must be made using the date1 & date2 columns, where date2 is the highest value for the number without exceeding date1 for that number.

The desired result from the above example would be like so.

 date1        text     number   ref
------------|--------|--------|-----
 2012-12-12 | hi     | 399    | 40
 2011-11-11 | so     | 399    | 50
 2010-10-10 | what   | 555    | null
 2009-09-09 | bye    | 300    | null
 2008-08-08 | you    | 300    | null

You can see in the result’s first row, ref is 40 was chosen because in table2 the record with ref=40 had a date2 that that was less than date1, and the highest date that met that condition.
In the result’s second row, ref is 50 was chosen because in table2 the record with ref=50 had a date2 that that was less than date1, and the highest date that met that condition.
The rest of the results have null refs because date1 is always less or a corresponding number doesn’t exist in table2.

I’ve got to a certain point but I’m stuck. The query I have so far is like this.

SELECT date1, text, number, ref
FROM table1
LEFT JOIN (
    SELECT * 
    FROM (
        SELECT * 
        FROM table2
        WHERE date2 <= '2012-12-12'
        ORDER BY date2 DESC
    ) tmp 
    GROUP BY msisdn
) tmp ON table1.number = table2.number;

The problem is that the hard coded date won’t do, it should be based on date1, but I can’t use date1 because it’s in the outer query. Is there a way I can make this work?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T23:58:23+00:00Added an answer on May 22, 2026 at 11:58 pm

    I tried similar example with different tables just now and was able to get what you wanted. Below is a similar query modified to fit your needs. You might want to change < with <= if that is what you are looking for.

    SELECT a.date1, a.text, b.ref
    FROM table1 a LEFT JOIN table2 b ON 
                            ( a.number = b.number
                              AND  a.date1 > b.date2 
                              AND b.date2 = ( SELECT MAX(x.date2) 
                                              FROM table2 x 
                                              WHERE x.number = b.number 
                                                 AND x.date2 < a.date1)
                            )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need some help with a MySQL query. I have two tables, one with
I need some help optimizing a MySQL query or table When I run this
Need some help with putting this query together. I'm using Mysql I have two
i need some help with this sql query heres my mysql table structure: DOMAINS
I need some help here. I have a mysql table called dictionary like this:
I'm working with jQuery for the first time and need some help. I have
I need some help on a MySQL query I'm trying to set up. I
I need some help with a MySQL query. I'm trying to rank participants using
I really need some help with forming a MySQL query that I just cannot
I'm very new to MySQL so I need some help please, I have a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.