我有一张这样的桌子:

+----+---------+------------+
| id | conn_id | read_date  |
+----+---------+------------+
|  1 |       1 | 2010-02-21 |
|  2 |       1 | 2011-02-21 |
|  3 |       2 | 2011-02-21 |
|  4 |       2 | 2013-02-21 |
|  5 |       2 | 2014-02-21 |
+----+---------+------------+

我想要第二高读取日期为特定的'康涅狄格的,即我想要一个组在康涅狄格。请帮我弄清楚这一点。

最佳答案

下面是一个特定conn_id的解决方案:

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
)

如果要使用conn_id为所有group by用户获取它,请执行以下操作:
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;

关于mysql - 从表中获取第二高的值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32175760/

10-15 18:13