根据MYSQL中每个ID的日期选择最新版本

时间:2016-02-18 09:14:04

标签: mysql sql max distinct mysql-num-rows

我有一张表:

+------------+-------------------+--------------+------------+
| listing_id | transaction_title | image_thumb  | sale_date  |
+------------+-------------------+--------------+------------+
| 226835186  | Title Version 11  | Img Style 11 | 2016-02-08 |
+------------+-------------------+--------------+------------+
| 226835186  | Title Version 11  | Img Style 12 | 2016-02-16 |
+------------+-------------------+--------------+------------+
| 228703248  | Title Version 21  | Img Style 21 | 2016-02-15 |
+------------+-------------------+--------------+------------+
| 228703248  | Title Version 22  | Img Style 22 | 2016-02-17 |
+------------+-------------------+--------------+------------+
| 228703248  | Title Version 23  | Img Style 21 | 2016-02-16 |
+------------+-------------------+--------------+------------+
| 230105831  | Title Version 31  | Img Style 31 | 2016-02-12 |
+------------+-------------------+--------------+------------+
| 230105831  | Title Version 32  | Img Style 31 | 2016-02-06 |
+------------+-------------------+--------------+------------+

我正在尝试使用最新使用版本的listing_idtransaction_title查询不同的image_thumb。对于上表,查询输出将为:

+------------+-------------------+--------------+------------+
| listing_id | transaction_title | image_thumb  | sale_date  |
+------------+-------------------+--------------+------------+
| 226835186  | Title Version 11  | Img Style 12 | 2016-02-16 |
+------------+-------------------+--------------+------------+
| 228703248  | Title Version 22  | Img Style 22 | 2016-02-17 |
+------------+-------------------+--------------+------------+
| 230105831  | Title Version 31  | Img Style 31 | 2016-02-12 |
+------------+-------------------+--------------+------------+

我尝试了select distinct, num_rows and max()的不同组合,但无法获得所需的结果。

最新我尝试过:

SELECT
    listing_id,transaction_title,image_thumb,sale_date
FROM (
    SELECT * FROM sales
    ORDER BY sale_date DESC
) AS transaction_title
GROUP BY listing_id

请帮忙!

6 个答案:

答案 0 :(得分:1)

您可以使用相关查询来选择每个查询的最大日期,如下所示:

SELECT listing_id,transaction_title,image_thumb,sale_date
FROM sales t
WHERE (listing_id,sale_date) in (select s.listing_id,max(s.sale_date)
                                 from sales s
                                 where t.listing_id = s.listing_id
                                 group by s.listing_id)

答案 1 :(得分:1)

您可以使用包含每listing_id个最大日期的派生表。如果你INNER JOIN到这个表,你可以获得exprescted结果集:

select t1.listing_id, transaction_title, image_thumb, sale_date
from mytable as t1
inner join (
   select listing_id, max(sale_date) max_date
   from mytable
   group by listing_id
) as t2 on t1.listing_id = t2.listing_id and sale_date = max_date

答案 2 :(得分:1)

您可以使用row_number基本方法,首先对listing_id和sale_date进行降序排序,然后使用row_number 1选择行。这将为您提供所需的数据集。此方法的查询模板如下:

SELECT  INVW.listing_id, INVW.transaction_title, INVW.image_thumb, INVW.sale_date 
FROM (
    SELECT  listing_id, transaction_title, image_thumb, sale_date
        ,@rank := if(@listing_id = listing_id or listing_id is null, @rank + 1, 1) as row_number 
        ,@listing_id := listing_id as dummy
    FROM <###REPLACE_ME_WITH_TABLE_NAME###>, (select @rank := 0,@listing_id := '') rank
    ORDER BY listing_id,sale_date DESC 
) INVW where INVW.row_number = 1;

答案 3 :(得分:0)

只需使用子查询获取listing_id,然后首先获取max sale_date,然后使用它查找其他列

select * 
  from table t0 
 where exists
   (select 1 from 
      (select listing_id,max(sale_date) as sale_date 
         from table group by listing_id) t1 
        where t1.listing_id = t0.listing_id and t1.sale_date = t0.sale_date)

答案 4 :(得分:0)

您可以使用相关子查询获得预期结果。

SELECT
    s1.listing_id, s1.transaction_title, s1.image_thumb, s1.sale_date
FROM sales s1
WHERE ( s1.listing_id, s1.transaction_title, s1.image_thumb, s1.sale_date ) IN   
                (SELECT s2.listing_id, s2.transaction_title, s2.image_thumb, s2.sale_date
                 FROM sales s2
                 WHERE s2.listing_id = s1.listing_id
                 ORDER BY s2.sale_date DESC
                 LIMIT 1);

答案 5 :(得分:-1)

由于未定义最大值之间的相关性,因此您只需为每列获取MAX值:

SELECT
    listing_id,
    MAX(transaction_title) transaction_title,
    MAX(image_thumb) image_thumb,
    MAX(sale_date) sale_date
FROM
    sales
GROUP BY
    listing_id