[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Get 2 most recent dates entered from 2 almost similar tables

Posted on 2012-09-20
12
Medium Priority
?
572 Views
Last Modified: 2012-09-23
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
0
Comment
Question by:peps03
  • 5
  • 5
  • 2
12 Comments
 
LVL 25

Expert Comment

by:lwadwell
ID: 38420100
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
 

Author Comment

by:peps03
ID: 38420114
Thanks for your reaction lwadwell!

oeps, forgot to mention, they can't be joined, and most columns have the same name.
0
 
LVL 25

Expert Comment

by:lwadwell
ID: 38420122
If the tables cannot be joined then I am confused.  What column in the other table makes it the most recent?
0
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 

Author Comment

by:peps03
ID: 38420145
they both have a column named lastmod
0
 
LVL 25

Accepted Solution

by:
lwadwell earned 600 total points
ID: 38420184
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
 
LVL 111

Assisted Solution

by:Ray Paseur
Ray Paseur earned 300 total points
ID: 38420508
Why not just run two SELECT queries?
0
 
LVL 25

Expert Comment

by:lwadwell
ID: 38420577
>> 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
 

Author Comment

by:peps03
ID: 38420967
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
 

Author Comment

by:peps03
ID: 38421085
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
 

Author Closing Comment

by:peps03
ID: 38421274
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
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 38421765
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
 
LVL 25

Expert Comment

by:lwadwell
ID: 38427306
>> 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

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
Creating a Cordova application which allow user to save to/load from his Dropbox account the application database.
The viewer will learn how to dynamically set the form action using jQuery.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
Suggested Courses

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question