select max (read_date) from my_table
where conn_id=1
and read_date<(
select max (read_date) from my_table
where conn_id=1
)
如果您想使用group by为所有conn_id获取它,请执行以下操作:
select t.conn_id, (select max(i.read_date) from my_table i
where i.conn_id=t.conn_id and i.read_date<max(t.read_date))
from my_table t group by conn_id;
$sql_max = '(select conn_id, max(read_date) max_date from tab group by 1) as tab_max';
$sql_max2 = "(select tab.conn_id,max(tab.read_date) max_date2 from tab, $sql_max
where tab.conn_id = tab_max.conn_id and tab.read_date < tab_max.max_date
group by 1) as tab_max2";
$sql = "select tab.* from tab, $sql_max2
where tab.conn_id = tab_max2.conn_id and tab.read_date = tab_max2.max_date2";
3条答案
按热度按时间hwamh0ep1#
以下是针对特定
conn_id
的解决方案:如果您想使用
group by
为所有conn_id
获取它,请执行以下操作:hrirmatl2#
以下答案应适用于MSSQL:
这里有一篇关于在MySQL中使用排名函数的有趣文章:
ROW_NUMBER() in MySQL
6qfn3psc3#
如果您的表格设计为ID -日期匹配(即一个大ID总是一个大日期),您可以按ID分组,否则请执行以下操作: