简体   繁体   中英

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?

您可以查询最长时间,然后选择每个匹配的帖子。

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

是的,您所需要做的就是检索max time_of_insertion并在您的位置使用它,如下所示:

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

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
  )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM