[postgresql 教程]postgresql 指定相同字段查出最后一组的记录

更新时间:2019-09-30    来源:PostgreSQL    手机版     字体:

【www.bbyears.com--PostgreSQL】

标题起的相当纠结,还是举例子吧

就是我有一堆记录,

created_date|   key | content |
1           |   test| xx?xxxxx|
2           |   test| xxxxx?xx|
3           |   test| ?xxxxxxx|
2           |   hah | xxxxxxx?|
3           |   hah | xxxx?xxx|

我想取出 key 分别为 test 和 haha 的 那一条.

created_date|   key | content |
3           |   test| ?xxxxxxx|
3           |   hah | xxxx?xxx|

不能用 group,因为我要取 content

终于把问题描述清楚了, sql 就是累

可以这样来解决(我以前在 oralce 下 exist 用的刷刷刷的,现在迅速遗忘了):

SELECT *
 FROM comment c
 WHERE NOT EXISTS (
   SELECT *
   FROM comment c2
   WHERE c.key = c2.key
     AND c2.created_date > c.created_date
   );

本文来源:http://www.bbyears.com/shujuku/70277.html

热门标签

更多>>

本类排行