简体   繁体   English

MySQL排名前十

[英]MySQL Sorting By Top 10

I am in the midst of creating a workout log for a team of mine. 我正在为我的团队创建锻炼日志。 What I currently have is a MySQL query (written in PHP) that retrieves an array of user ids associated with posts made in the current week. 我目前拥有的是一个MySQL查询(用PHP编写),该查询检索与当前一周内发布的帖子相关的用户ID数组。 The code is as follows: 代码如下:

mysql_query("SELECT u_id FROM posts WHERE id IN (SELECT p_id FROM post_groups WHERE g_id='" . $group['id']. "') AND date >= '" . $d_start . "' AND date <= '" . $d_end . "'");

To better explain this, say a user with u_id = 6 posts five workouts this week. 为了更好地解释这一点,假设一个u_id = 6的用户本周发布了五次锻炼。 The array generated by the above query should contain 6 a total of five times. 由上述查询生成的数组应包含6 ,总共5次。 This is done for every workout posted by users in the week - so I end up with a large array full of duplicate user ids. 这是针对用户在一周内发布的所有锻炼而完成的-因此,最终我得到了一大堆重复的用户ID。

What I am looking to do is, instead, return an array of size 10 that contains the u_id s that appear most frequently in the array above. 我想要做的是,返回一个大小为10的数组,其中包含在上面的数组中出现频率最高的u_id So, if u_id = 6 appeared the most frequently in the above array, let's say 10 times, and u_id = 8 appeared the second most frequently, let's say 9 times, then the array that I wish to return would have a value of 6 at [0] and a value of 8 at [1], etc. 因此,如果u_id = 6在上面的数组中出现频率最高,比如说10次,而u_id = 8在第二个数组中出现频率第二高,比如说9次,那么我希望返回的数组的值为6 [0]和[1]处的值为8 ,依此类推。

I am not sure how to trim down this array and sort it by most frequent occurrences of u_id , as my SQL skills are a little lacking at this moment. 我不确定如何缩减此数组并按u_id的最频繁出现对其进行u_id ,因为目前我的SQL技能还有些不足。 I was hoping someone would be able to shed some light on the ideal query that I am looking for. 我希望有人能够阐明我正在寻找的理想查询。 I appreciate your help! 我感谢您的帮助!

It sounds like you want to use both GROUP BY , ORDER BY , and TOP . 听起来您想同时使用GROUP BYORDER BYTOP

GROUP BY will group your results by a particular field, in this case you want to be grouping by u_id. GROUP BY将按特定字段将结果分组,在这种情况下,您要按u_id分组。

GROUP BY u_id

ORDER BY lets you order your resultset. ORDER BY使您可以订购结果集。 In this example you want to group by the COUNT() of how many rows each user has. 在此示例中,您COUNT()每个用户有多少行的COUNT()分组。 We also use the DESC keyword to order them in descending order (highest to lowest). 我们还使用DESC关键字对它们进行降序排列(从最高到最低)。

ORDER BY COUNT(u_id) DESC

TOP lets you only get the top X results. TOP只让您获得前X个结果。 In the example below only the top 10 rows will be returned. 在下面的示例中,仅返回前10行。

TOP 10

So putting it all together, to group by user id, order by the count of each userid in descending order and to limit the results to 10: 因此,将所有内容放在一起,以用户ID分组,以每个用户ID的降序排列,并将结果限制为10:

SELECT u_id 
FROM posts 
WHERE id IN 
  (SELECT p_id 
   FROM post_groups 
   WHERE g_id='" . $group['id']. "') 
AND date >= '" . $d_start . "' 
AND date <= '" . $d_end . "'
GROUP BY u_id
ORDER BY COUNT(u_id) DESC
LIMIT 10

On a side note... you really shouldn't be using the mysql_* functions in new code . 附带说明...实际上不应该在新代码中使用mysql_ *函数 They are no longer maintained and are officially deprecated (meaning they'll be removed in a future version of PHP). 它们不再被维护并且已正式弃用 (这意味着它们将在以后的PHP版本中删除)。 Instead look into using prepared statements with either MySQLi or PDO . 而是考虑将准备好的语句与MySQLiPDO一起使用

There are plent of good examples of using PDO here , and here on using MySQLi . 这里有很多使用PDO的好例子, 这里还有使用MySQLi的好例子。

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

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