Get 2 most recent dates entered from 2 almost similar tables

Hi,

I have 2 almost similar tables, they each have 1 column which is different, with the column "last modified".

I would like to get the 2 most recent values from the 2 tables together.

How should i do that?

Thanks
peps03Asked:
Who is Participating?
 
lwadwellCommented:
If you specify the same number of columns and they have the same relative datatypes ... perhaps a UNION ALL is what you want

SELECT col1, col2, col3, lastmod
FROM table1
UNION ALL
SELECT col1, col2, col3, lastmod
FROM table2
ORDER BY lastmod
LIMIT 2
0
 
lwadwellCommented:
Not knowing the table ... and assuming that you can join them as they hold the same keys, perhaps

SELECT *
FROM table1 INNER JOIN table2 ON table1.key = table2.key
ORDER BY "last modified" DESC
LIMIT 2
0
 
peps03Author Commented:
Thanks for your reaction lwadwell!

oeps, forgot to mention, they can't be joined, and most columns have the same name.
0
Network Scalability - Handle Complex Environments

Monitor your entire network from a single platform. Free 30 Day Trial Now!

 
lwadwellCommented:
If the tables cannot be joined then I am confused.  What column in the other table makes it the most recent?
0
 
peps03Author Commented:
they both have a column named lastmod
0
 
Ray PaseurCommented:
Why not just run two SELECT queries?
0
 
lwadwellCommented:
>> Why not just run two SELECT queries?
Good question ... I was assuming the OP wants the two most recent across both tables, not the two most recent from each.  Maybe I assumed wrong ... perhaps the OP can clarify.
0
 
peps03Author Commented:
Yes, i would like the 2 most recent (in total) from the 2 tables.

lwadwell, your reply @ ID: 38420184 only works if the columns are the same on both tables. They are not, both tables have a column i need to use that is aberrant.

Maybe 2 select queries is the best option..
0
 
peps03Author Commented:
but when using 2 select queries, i should get the 2 most recent dates from each table and compare the 4 values and get the 2 highest.

arraytable1[0] = value
arraytable1[1] = value

arraytable2[0] = value
arraytable2[1] = value

how should i get the 2 highest values, including the corresponding array key?
0
 
peps03Author Commented:
Fixed it eventually, with 3 select queries. first getting all the mod-dates, next by matching the dates in table 1 than by matching the dates in table 2.

thanks for the tips.
0
 
Ray PaseurCommented:
Thanks for the points.  I think this construct is close but not exact:

arraytable1[0] = value
arraytable1[1] = value

arraytable2[0] = value
arraytable2[1] = value

Instead you might do two SELECT queries to acquire the two highest date values from each table, putting the results into a single array keyed by the date values.  Then you can ksort() the array, and use array_pop() twice.

Anyway, best of luck with it, ~Ray
0
 
lwadwellCommented:
>> lwadwell, your reply @ ID: 38420184 only works if the columns are the same on both tables. They are not, both tables have a column i need to use that is aberrant.
I know that the columns have to be the same - I said that in my post.  Sorry I was not clear enough ... you specify the actual columns of interest (not select *) that are common to both tables.  You could even have added a value of you own to help id the table ... that an the PK you would have
SELECT 'table1' as tname, ID, lastmod
FROM table1
UNION ALL
SELECT 'table2' as tname, ID, lastmod
FROM table2
ORDER BY lastmod
LIMIT 2

This would then have given a pointer to the table and the PK to get the remaining data columns using another SELECT (or the UNION ALL could have been in an inline view).
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.