为不同的条目选择最新的条目 [英] Selecting latest entries for distinct entry

查看:44
本文介绍了为不同的条目选择最新的条目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于我将如何做到这一点,我脑子里放了个屁.我只需要选择一组相同 ID 条目中的最新条目

Im having a brain fart as to how I would do this. I need to select only the latest entry in a group of same id entries

我在约会表中有记录.

  lead_id   app_id
     4        42
     3        43
     1        44
     2        45
     2        46 (want this one)
     1        48
     3        49 (this one)
     4        50 (this one)
     1        51 (this one)

我需要的结果是 app_id 46,49,50,51

The results I require are app_id 46,49,50,51

仅约会表中的最新条目,基于重复的 Lead_id 标识符.

Only the latest entries in the appointment table, based on duplicate lead_id identifiers.

推荐答案

我认为这是更优化和更有效的方法(排序下一个分组):

I think this is much more optimal and efficient way of doing it (sorting next grouping):

SELECT * FROM (
    SELECT * FROM appointment
    ORDER BY lead_id, app_id DESC 
) AS ord
GROUP BY lead_id 

当您还需要表中的所有其他字段而无需复杂查询时,这将非常有用

this will be useful when you need all other fields too from the table without complicated queries

结果:

lead_id     app_id
1           51
2           46
3           49
4           50

这篇关于为不同的条目选择最新的条目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆