选择具有最新时间戳的记录

时间:2014-01-17 08:16:12

标签: mysql sql database time timestamp

我有两个表,来自女巫想要选择用户,系统和软

记录应该是最新的记录“ tstamp2

第一:表系统

USER       SYSTEM           ltstamp           
======-----======----===================
User1       LA1      2013-05-06 11:27:26
User2       LA2      2013-06-07 11:27:26 

第二:表软件

Soft     SYSTEM            tstamp2
=====----=====------===================
  Av1      LA1       2013-04-06 10:27:26
  Av2      LA1       2013-05-06 11:27:26
  Av1      LA2       2013-04-06 10:27:26
  Av2      LA2       2013-06-07 11:27:26

3 个答案:

答案 0 :(得分:3)

SELECT s.user, s.system, sw.max_tstamp, sw2.soft
FROM
  systems s INNER JOIN (SELECT system, MAX(tstamp2) AS max_tstamp
                        FROM software
                        GROUP BY system) sw
  ON s.system = sw.system INNER JOIN software sw2
  ON s.system = sw2.system AND sw.max_tstamp=sw2.tstamp2

请参阅小提琴here

答案 1 :(得分:1)

您需要一个子请求才能执行此操作。例如:

select * from systems 
where ltstamp = (select top 1 ltstamp from systems order by ltstamp desc)

答案 2 :(得分:1)

试试这个,
select user, system,soft , min(tstamp2) tstamp2 from systems s inner join software w on s.SYSTEM=w.SYSTEM group by user