简体   繁体   English

如何按用户ID分组并按desc排序

[英]How to Group by user_id and order by desc

Hello i am aware this is very basic but right now i am so confused i want to GROUP BY user_id ORDER BY id DESC LIMIT 4 (i have posted short example of table and my query) 您好我知道这是非常基本的但是现在我很困惑我想要GROUP BY user_id ORDER BY id DESC LIMIT 4(我已经发布了表和我的查询的简短示例)

My table looks like where id is auto increment i only save user_id i only want to take one user_id only once which is lastest entry in database ignore other. 我的表看起来像id是自动增量我只保存user_id我只想要一个user_id只有一次,这是数据库中最新的条目忽略其他。

Table
---------------------
id     |   user_id  |
---------------------
13     |    25      |
12     |    36      |
11     |    25      |
10     |    42      |
9      |    95      |
8      |    25      |
7      |    95      |
---------------------
so on

I have tried this 我试过这个

SELECT * FROM  `table` GROUP BY user_id ORDER BY `id` DESC LIMIT 4

I want it to output 25,36,42,95 i have also tried many experiments but nothing seems to be working. 我希望它输出25,36,42,95我也尝试了很多实验,但似乎没有任何工作。 Do i need timestamp or something to make it in group? 我是否需要时间戳或其他东西来组成团体? or what query will work? 或者什么查询将起作用?

You are doing a partial GROUP BY which does not work the way you expect. 你正在做一个部分GROUP BY ,它不会按你期望的方式工作。 Here is a query which produces the desired results: 这是一个产生所需结果的查询:

SELECT MAX(id) AS MAXID, user_id
FROM `table`
GROUP BY user_id
ORDER BY MAXID DESC
LIMIT 4

The behavior is explained here : 这里解释了这种行为:

MySQL extends the use of GROUP BY so that the select list can refer to nonaggregated columns not named in the GROUP BY clause. MySQL扩展了GROUP BY的使用,因此选择列表可以引用GROUP BY子句中未命名的非聚合列。 [...] You can use this feature to get better performance by avoiding unnecessary column sorting and grouping. [...]您可以通过避免不必要的列排序和分组来使用此功能来获得更好的性能。 However, this is useful primarily when all values in each nonaggregated column not named in the GROUP BY are the same for each group. 但是,当GROUP BY中未命名的每个非聚合列中的所有值对于每个组都相同时,这非常有用。 The server is free to choose any value from each group, so unless they are the same, the values chosen are indeterminate. 服务器可以自由选择每个组中的任何值,因此除非它们相同,否则所选的值是不确定的。 Furthermore, the selection of values from each group cannot be influenced by adding an ORDER BY clause. 此外,添加ORDER BY子句不会影响每个组中值的选择。 Sorting of the result set occurs after values have been chosen, and ORDER BY does not affect which values within each group the server chooses. 选择值后会对结果集进行排序,而ORDER BY不会影响服务器选择的每个组中的值。

Try this 尝试这个

SELECT MAX(id) as countid , `user_id` from `table`
GROUP BY `user_id`
ORDER BY countid  DESC
LIMIT 20
  1. In your example you need to get result like 25,36,42,95 , but sort by ID field. 在您的示例中,您需要获得结果,如25,36,42,95 ,但按ID字段排序。 If you will sort by it you will get different result. 如果你按它排序,你会得到不同的结果。

  2. You get a grouping in your query so you cannot use columns that not chosen in query (id is ommited there) 您在查询中得到一个分组,因此您不能使用未在查询中选择的列(id在那里被省略)

  3. If you still need to order by id, i think that you need to add order by MAX('id') or MIN('id') 如果您仍需要按ID订购,我认为您需要按MAX('id')或MIN('id')添加订单


In your case i`ll make next query: 在你的情况下,我将进行下一个查询:

SELECT user_id FROM  `table` GROUP BY user_id ORDER BY MAX(`id`) DESC LIMIT 4

而不是分组,你可以使用distinct ,如下面的SQL查询。

SELECT distinct(user_id) FROM  `table` ORDER BY `id` DESC LIMIT 4

SELECT * FROM(SELECT 13 id,25 UserID UNION ALL SELECT 12 id,36 UserID UNION ALL SELECT 11 id,25 UserID UNION ALL SELECT 10 id,42 UserID UNION ALL SELECT 09 id,95 UserID UNION ALL SELECT 08 id,25 UserID UNION ALL SELECT 07 id,95 UserID)GROUP BY用户ID ORDER BY id DESC LIMIT 4

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

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