简体   繁体   English

按两列排序MySQL

[英]Order by two columns MySQL

I am implementing a Popular feature into my app where I will select the most popular posts from a database. 我在我的应用程序中实现了“流行”功能,从数据库中选择最受欢迎的帖子。 Problem is I need to get the most popular in terms of most likes and most comments. 问题是我需要在最喜欢和最喜欢的评论中获得最受欢迎。 I just tried: 我只是试过:

SELECT * FROM db ORDER BY `likesCount`,`commentsCount` DESC LIMIT $from," . ($to-$from)

But that will show an ascending value in terms of the numberOfLikes. 但这将在numberOfLikes方面显示出递增的值。 How can I get it so that it measures the numberOfLikes and numberOfComments and selects the most popular one? 我如何获得它以便度量numberOfLikes和numberOfComments并选择最受欢迎的一个?

You can specify whether you want to sort in ascending or descending order for each search field. 您可以为每个搜索字段指定是按升序还是降序排序。

SELECT * FROM `db` ORDER BY `likesCount` DESC, `commentsCount` DESC

With that said, you won't be able to select the most like and most commented ones at the same time. 这样一来,您将无法同时选择最喜欢和评论最多的。 You'll have to come up with a formula to figure that out. 您必须想出一个公式来解决这个问题。 For instance, the example below find the maximum value for a given "id" between the number of likes and comments: 例如,以下示例在喜欢和评论的数量之间找到给定“ id”的最大值:

SELECT * FROM `db` GROUP BY `id` ORDER BY GREATEST(`likesCount`, `commentsCount`) DESC

Another alternative would be to add them together: 另一种选择是将它们添加在一起:

SELECT * FROM `db` ORDER BY `likesCount` + `commentsCount` DESC

Don't you want to order by total number of likes and comments? 您不想按喜欢和评论的总数排序吗? Does one of these weigh more than the other when it comes to popularity? 在普及方面,其中一个比另一个重吗?

You could just do: 您可以这样做:

SELECT *, (`likesCount` + `commentsCount`) as popularity FROM db ORDER BY popularity DESC ...

As mistercruffles suggested below, you can add in a multiplier to have one factor weigh more than the other. 如下面的建议,您可以添加一个乘数,使一个因素的权重大于另一个因素。 Just change ( likesCount + commentsCount ) with (X* likesCount + Y* commentsCount ) where X is the factor for likes and Y is for comments. 只需使用(X * likesCount + Y * commentsCount )更改( likesCount + commentsCount ),其中X是喜欢的因素,Y是评论的因素。 For X = 2 and Y = 1, it means that likes are two times more important than comments, but I'm sure you figured that out. 对于X = 2和Y = 1,这表示赞的重要性比评论重要两倍,但是我敢肯定,您知道了。

If you want to weight likes more than comments, you can use: 如果您想称赞多于评论,则可以使用:

SELECT * FROM `db` ORDER BY (3*`likesCount` + 2*`commentsCount`) DESC

Which will weight likes more than comments in a 3:2 ratio, ie 50% stronger. 权重比评论更喜欢3:2的比例,即强50%。 I'm not sure whether you want this or are would prefer to choose the higher of the two counts to rank by, but want to weight them before determining the higher of the two. 我不确定您是否要这样做,还是更愿意选择两个计数中的较高者进行排名,但想在确定两个计数中的较高者之前对其进行加权。 If the latter is what you are looking for then this should do the trick: 如果您正在寻找后者,那么这应该可以解决问题:

SELECT * FROM `db` ORDER BY greatest(3*`likesCount`, 2*`commentsCount`) DESC

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

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