沿其他记录的聚合功能

时间:2016-06-27 11:42:03

标签: php mysql aggregate

假设我要获取表T1的所有记录。此外,我需要访问记录的minmax日期值。

我可以使用两个查询来做到这一点:

select * from T1 ;
select min(created_at) as min_date,max(created_at) as max_date from T1;

这是两个单独的查询,但是可以将它们放在一个查询中吗?  我的意思是所有记录加上特定列的最小值和最大值。

2 个答案:

答案 0 :(得分:3)

select *,
       (select min(created_at) from T1) as min_date,
       (select max(created_at) from T1) as max_date
from T1;

答案 1 :(得分:0)

SELECT * FROM `T1` JOIN (SELECT MIN(`created_at`) AS min_date,
MAX(`created_at`) AS max_date FROM `T1` ) AS temp