4

I have a mySQl db (name "stocks") with 50 tables, each tables with

id, symbol, date, time, open, high, low, close, volume as columns (9 columns).

I would like to know what is the last record for each table, ordered for date then time.

Should I have to ORDER BY all data for each table or there is a better way to just know last record?

I am asking help for a query that just return only last record for each table in db.

Thanks

PS For last record I mean most recent as Date then Time

4
  • Why aren't you using DATETIME datetime instead of DATE date, TIME time? Commented Feb 2, 2012 at 15:58
  • Oh it is just my preference to have separate columns. Commented Feb 2, 2012 at 16:03
  • 1
    It's my personal opinion and it's nothing wrong on how you do have it but I'd rather use DATETIME, you can easily use TIME( datetime) or DATE( datetime) to get those information separately, and on the other hand sorting data based on time would be easier and perhaps faster. Commented Feb 2, 2012 at 16:07
  • Thank you very much Vyktor for your comment Commented Feb 2, 2012 at 16:16

3 Answers 3

11

There are two options how to do that:

-- I would use this only if you need more than one records
SELECT * FROM table ORDER BY date DESC LIMIT 1;

-- Way to go:
SELECT * FROM table WHERE date = (SELECT MAX(date) FROM table) LIMIT 1;

Don't forget to add index on date. If it's possible you add lot's of records at the same time you will have to add:

ORDER BY id DESC -- In case that date is highest for records for last records
ORDER BY time DESC -- Every other case

To the end of query

Sign up to request clarification or add additional context in comments.

3 Comments

You'd probably want to check time as well as date, in case there are multiple records with the same value and they are all max(date). Of course, if there are many inserts happening at the same time, there could be collisions on time as well, unless time has microsecond (or better) precision.
@FrustratedWithFormsDesigner somehow I assumed that he would be using DATETIME in case of many records per day... But you're right, I've added handling of records (id seems most reliable quantifier on "one table query", assuming it's create data, heh).
ORDER BY Date DESC, Time DESC is probably what the OP wants (see his edit).
7

I am going to make the assumption that the record with the largest ID is the "last" (assuming strictly increasing sequential IDs that are unique within a table). If you have a better definition of "last" that could make a difference.

To get one "last" record, you could do:

Select * from table_1 where id = (select max(id) from table_1);

To get the results of all 50 tables into a single result set, you could do:

Select * from table_1 where id = (select max(id) from table_1)
union
Select * from table_2 where id = (select max(id) from table_2)
union
Select * from table_3 where id = (select max(id) from table_3)
union...

A MySQL-specific solution could be

Select * from table_1 order by id desc limit 1
union
Select * from table_2 order by id desc limit 1
union
Select * from table_3 order by id desc limit 1
union...

Based on your edit (where you actually define what you mean by "last"):

Select * from table_1 order by date desc, time desc, id desc limit 1
union
Select * from table_2 order by date desc, time desc, id desc limit 1
union
Select * from table_3 order by date desc, time desc, id desc limit 1
union...

2 Comments

SELECT * FROM table_1 ORDER BY id DESC LIMIT 1 gives same result in MySQL.
@ypercube: True, though since I don't have an instance of MySQL to test on, this solution is probably the most generic.
0

Here is one way to do it without sorting the table:

select * from tab1
 where time = (select max(time)
                 from tab1
                where date = (select max(date) from tab1))
   and date = (select max(date) from tab1)

It should be very fast, like, O(c), provided that both columns are indexed, otherwise the time will simply be O(n)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.