Fane
Fane

Reputation: 2074

Select all rows from last inserted

What I'd like to do is retrieve ALL the posts that have the last time inserted. So:

time_of_insertion | content
        x              --
       x+1             --
       x+1             --
       x+1             --
       x+2             --
       x+2             --      
       x+2             --
       x+2             --

What I'd like to do is obtain the last two posts with x+2 because they both are the last inserted. Can this be done? Thank you very much...

EDIT: sorry guys I forgot to mention I wanted this on an union

Supposing I want the MAX(last time_of_insertion) from two different tables. So if table1 has x+1 time of insertion and table2 has x+3, I want x+3 from table2, not table1. Any ideas?

Upvotes: 0

Views: 47

Answers (3)

xQbert
xQbert

Reputation: 35323

After several formatting errors and incorrect / missing inline table aliases...

SELECT * 
FROM (
  SELECT TIME 
  FROM table1 
  UNION ALL 
  SELECT TIME 
  FROM table2 
  ) B 
WHERE TIME = (
  SELECT MAX(TIME) 
  FROM (
    SELECT TIME 
    FROM table1 
    union all 
    SELECT TIME 
    FROM table2) a
  )

Upvotes: 0

Mike Feltman
Mike Feltman

Reputation: 5176

Yes, all you need to do is retrieve the max time_of_insertion and use it in your where like this:

select * from table where time_of_insertion = (SELECT MAX(time_of_insertion) from table) ;

Upvotes: 0

dognose
dognose

Reputation: 20889

You can just query the max time and then pick every post matching.

SELECT * FROM table WHERE time_of_insertion = (SELECT MAX(time_of_insertion) FROM table);

Upvotes: 2

Related Questions