简体   繁体   English

获得MySQL中最顶尖的记录

[英]Getting top distinct records in MySQL

This is probably something very simple, so forgive my blonde moment :) 这可能是非常简单的事情,所以原谅我的金发时刻:)

I have a table 'album' 我有一张桌子'专辑'

* albumId
* albumOwnerId (who created)
* albumCSD (create stamp date)

Now what I am trying to do is to select the top 10 most recently updated albums. 现在我要做的是选择前10个最近更新的专辑。 But, I don't want 10 albums from the same person coming back - I only want one album per unique person. 但是,我不希望同一个人的10张专辑回来 - 我只想要每个独特的人一张专辑。 IE 10 albums from 10 different people. 来自10个不同人的IE 10专辑。

So, this is what I have below, but it is not working properly and I just can't figure out why. 所以,这就是我在下面所拥有的,但它没有正常工作,我只是无法弄清楚为什么。 Any ideas? 有任何想法吗?

Thanks 谢谢

SELECT DISTINCT(albumOwnerId), albumId
FROM album
ORDER BY albumCSD DESC
LIMIT 0,10

Here is some example data, followed by what I am trying to get. 这是一些示例数据,接下来是我想要获得的数据。 Hope this makes it clearer. 希望这更清楚。

DATA: 数据:

albumOwnerID, albumId, albumCSD
18, 194, '2010-10-23 11:02:30'
23, 193, '2010-10-22 11:39:59'
22, 192, '2010-10-12 21:48:16'
21, 181, '2010-10-12 20:34:11'
21, 178, '2010-10-12 20:20:16'
19, 168, '2010-10-12 18:31:55'
18, 167, '2010-10-11 21:06:55'
20, 166, '2010-10-11 21:01:47'
18, 165, '2010-10-11 21:00:32'
20, 164, '2010-10-11 20:50:06'
17, 145, '2010-10-10 18:54:24'
17, 144, '2010-10-10 18:49:28'
17, 143, '2010-10-10 18:48:08'
17, 142, '2010-10-10 18:46:54'
16, 130, '2010-10-10 16:17:57'
16, 129, '2010-10-10 16:17:26'
16, 128, '2010-10-10 16:07:21'
15, 119, '2010-10-10 15:24:28'
15, 118, '2010-10-10 15:24:11'
14, 100, '2010-10-09 18:22:49'
14, 99, '2010-10-09 18:18:46'
11, 98, '2010-10-09 15:50:13'
11, 97, '2010-10-09 15:44:09'
11, 96, '2010-10-09 15:42:28'
11, 95, '2010-10-09 15:37:25'

DESIRED DATA: 所需数据:

18, 194, '2010-10-23 11:02:30'
23, 193, '2010-10-22 11:39:59'
22, 192, '2010-10-12 21:48:16'
21, 181, '2010-10-12 20:34:11'
19, 168, '2010-10-12 18:31:55'
17, 145, '2010-10-10 18:54:24'
16, 130, '2010-10-10 16:17:57'
15, 119, '2010-10-10 15:24:28'
14, 100, '2010-10-09 18:22:49'
11, 98, '2010-10-09 15:50:13'

I get results, you want to have, with this query 通过此查询,我得到了您想要的结果

SELECT albumOwnerID, albumId, albumCSD
FROM album
WHERE albumCSD in
(SELECT  Max(album.albumCSD) AS MaxvonalbumCSD
FROM album
GROUP BY album.albumOwnerID);

However in MS Access 但是在MS Access中

select albumOwnerID, albumID
from album
Group by albumOwnerID, albumID
Order by albumcsd desc
LIMIT 0,10

EDIT: 编辑:

select albumOwnerID, albumID 
from album
where albumOwnerID in (select distinct albumOwnerID from album order by albumCSD )
LIMIT 0,10

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM