Jump to content

Mysql query - need to get the latest result set when compared with two tables


seenu_vas80

Recommended Posts

HI,

 

  Iam facing a problem while comparing two tables to get the latest result from the table 2. I have mentioned the problem in detail below.

 

Table1

Id Name ver_no Flag Attempt

1 Lorem Ipsum EN001 1 6

2 Lorem Ipsum EN002 2 5

 

 

Table2

Id Text_val Ver_no Status Doa

1 Message1 EN001 A 2009-05-26

2 Message 2 EN001 A 2009-05-26

3 Message 3 EN001 A 2009-05-27

4 Message 4 EN002 A 2009-05-27

5 Message 5 EN001 A 2009-05-27

6 Message 6 EN002 A 2009-05-27

 

By comparing these two tables, we need to get the latest record in the table2 for each and every version. We have tried by using “Group by” clause of the field ‘ver_no’ which is common to both the tables. While doing this we are getting the record which has been inserted at the beginning of each ver_no and not the latest one. 

 

While running the query we need the result row as:

Id Text_val Ver_no doa

5 Message 5 EN001 2009-05-27

6 Message 6 EN002 2009-05-27

 

 

I have listed the query which we have tried below:

 

Select table2.id, table2. Text_val, table2.ver_no, table2.doa from table1, table2 where table1.ver_no = table2.ver_no group by table2.ver_no

 

These query returns the first entry of the version in the table2 as a result. But I need the latest record that has been inserted in the table2 as the result.

 

I have tried with the table operations also. but i couldn't get the result which i expect.

 

Need help on this.

 

 

 

Link to comment
Share on other sites

Thanks for reply.

I have tried this.But I'm getting the following result.

 

While running the query we need the result row as:

Id  Text_val  Ver_no  doa

1  Message 5  EN001  2009-05-26

4  Message 6  EN002  2009-05-27

 

Need help on this..

Link to comment
Share on other sites

Hi

 

Think what you want is the table1 record matched with the latest record from table2 for that ver_no.

 

As such:-

 

SELECT * 
FROM Table1
JOIN (SELECT Ver_no, Max( Doa ) AS Doa FROM Table2 GROUP BY Ver_no) Deriv1 
ON Table1.Ver_no = Deriv1.Ver_no
JOIN Table2 
ON Deriv1.Ver_no = Table2.Ver_no
AND Deriv1.Doa = Table2.Doa

 

All the best

 

Keith

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.