Select 基于 MYSQL 中每个 ID 的日期的最新版本
Select the latest version based on date for each ID in MYSQL
我有一个table喜欢:
+------------+-------------------+--------------+------------+
| 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 |
+------------+-------------------+--------------+------------+
我正在尝试使用最新使用的 transaction_title
和 image_thumb
版本查询不同的 listing_id
。对于上面的 table 查询输出将是:
+------------+-------------------+--------------+------------+
| 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
请帮忙!
您可以使用相关查询 select 每个查询的最大日期,如下所示:
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)
先用子查询得到listing_id最大的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)
你可以使用相关子查询得到预期的结果。
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);
因为没有定义最大值之间的相关性,您可以简单地获取每一列的最大值:
SELECT
listing_id,
MAX(transaction_title) transaction_title,
MAX(image_thumb) image_thumb,
MAX(sale_date) sale_date
FROM
sales
GROUP BY
listing_id
您可以使用包含每个 listing_id
的最大日期的派生 table。如果你 INNER JOIN
这个 table 你可以得到预期的结果集:
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
您可以使用 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;
我有一个table喜欢:
+------------+-------------------+--------------+------------+
| 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 |
+------------+-------------------+--------------+------------+
我正在尝试使用最新使用的 transaction_title
和 image_thumb
版本查询不同的 listing_id
。对于上面的 table 查询输出将是:
+------------+-------------------+--------------+------------+
| 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
请帮忙!
您可以使用相关查询 select 每个查询的最大日期,如下所示:
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)
先用子查询得到listing_id最大的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)
你可以使用相关子查询得到预期的结果。
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);
因为没有定义最大值之间的相关性,您可以简单地获取每一列的最大值:
SELECT
listing_id,
MAX(transaction_title) transaction_title,
MAX(image_thumb) image_thumb,
MAX(sale_date) sale_date
FROM
sales
GROUP BY
listing_id
您可以使用包含每个 listing_id
的最大日期的派生 table。如果你 INNER JOIN
这个 table 你可以得到预期的结果集:
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
您可以使用 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;