[英]How do I get distinct rows by a column?
I have a huge number of rows that I'd like to get say, last 5 records inserted in that database from 10 different users. 我有很多行,我想说,从10个不同的用户插入该数据库的最后5条记录。 If the same user inserted the last 3 rows into database, we must get one row, skip the others two and move to get a row per user, until it count up to 5. 如果同一个用户将最后3行插入数据库,我们必须得到一行,跳过其他两行并移动以获得每个用户一行,直到它计数到5。
A database like that: 像这样的数据库:
user_id | news_id | title
1 | 1 | foo-1
2 | 2 | foo-2
3 | 3 | foo-3
1 | 4 | baa
4 | 5 | baa0
5 | 6 | baa1
5 | 7 | baa2
6 | 8 | baa3
7 | 9 | baa4
Should return: 应该返回:
user_id | news_id | title
1 | 1 | foo-1
2 | 2 | foo-2
3 | 3 | foo-3
4 | 5 | baa0
5 | 6 | baa1
The current filter was done by PHP, like this: 当前过滤器由PHP完成,如下所示:
$used = array();
while ($data = mysql_fetch_array($query)) {
$uid = $data['user_id'];
if(in_array($uid, $used))
continue;
array_push($used, $uid);
// do something with data
}
But I want to refactor it, and do the filter purely by mysql, if possible. 但是我想重构它,并且如果可能的话,完全由mysql做过滤器。 I don't know much MySql and that's why I'm having problem to archive this... 我不太了解MySql,这就是为什么我有问题归档这个...
Here's what I've tried 这是我尝试过的
select DISTINCT(user_id), news_id, title from XXX
WHERE GROUP BY (news_id) DESC
LIMIT 0,5
How can I do that? 我怎样才能做到这一点?
1 way you can do it is to generate a partitioned row number per user and then select 5 records where RowNumber = 1. 1种方法是为每个用户生成一个分区行号,然后选择5个记录,其中RowNumber = 1。
SELECT *
FROM
(
SELECT
d.user_id
,d.news_id
,d.title
,(@rn:= if(@uid = user_id, @rn + 1,
if(@uid:=user_id,1,1)
)
) as RowNumber
FROM
Data d
CROSS JOIN (SELECT @uid:=-1, @rn:=0) vars
ORDER BY
user_id
,news_id
) t
WHERE
t.RowNumber = 1
ORDER BY news_id
LIMIT 5;
http://rextester.com/JRIZI7402 - example to show it working http://rextester.com/JRIZI7402 - 示例显示它正常工作
Note you can change the row order by simply changing the ORDER BY statement of the derived table so if you have a column that will signify the latest record eg an identity column or a datetime column you can use that, but user_id must be the first criteria to be partitioned correctly. 注意,您可以通过简单地更改派生表的ORDER BY语句来更改行顺序,这样如果您有一个表示最新记录的列,例如您可以使用的标识列或日期时间列,但user_id必须是第一个标准要正确分区。
Do it from your query. 从您的查询中执行。
"SELECT * FROM table GROUP BY user_id ORDER BY news_id DESC LIMIT 5"
well, i think this will achieve what you are after. 好吧,我认为这将实现你所追求的目标。
select user_id, news_id, title from tableName
GROUP BY user_id
ORDER BY news_id DESC
LIMIT 0,5
Hope this helps! 希望这可以帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.