在SQL中获取具有唯一目标的最新数据

时间:2013-08-14 14:16:20

标签: mysql sql select group-by sql-order-by

我是MySQL的新手,我在mysql表中有这个表

我怎样才能产生这样的结果?

我想要的是获得具有唯一目的地和年份的最新时间戳。

我尝试过这个mysql查询,但似乎无法工作:

SELECT chp_destination, cde_name, chp_year, chp_from, chp_to, chp_budget_price_high, chp_medium_price_high, chp_luxury_price_high,
    chp_budget_price_low, chp_medium_price_low, chp_luxury_price_low, chp_timestamp, chp_comment, MAX( chp_id ), 
FROM crm_hotel_price
LEFT JOIN crm_destinations ON cde_id = chp_destination
GROUP BY chp_destination, chp_year
ORDER BY chp_id

1 个答案:

答案 0 :(得分:0)

试试这个:

SELECT * 
FROM (SELECT chp_id, chp_destination, cde_name, chp_year, chp_from, chp_to, chp_budget_price_high, 
                 chp_medium_price_high, chp_luxury_price_high, chp_budget_price_low, chp_medium_price_low, 
                 chp_luxury_price_low, chp_timestamp, chp_comment
        FROM crm_hotel_price
        LEFT JOIN crm_destinations ON cde_id = chp_destination
        ORDER BY chp_destination, chp_year, chp_id DESC) A 
GROUP BY chp_destination, chp_year