问题描述
我有一个包含评分项目的数据库.当我想显示特定类别中的所有项目并按比率而不是按赞数对结果进行排序时,这很容易并且可以正常工作:
I have a database with rated items in it. When I want to display all the items from a specific category and sort the results by rate than by number of likes, it's easy and it's working:
$query = "SELECT * FROM infos WHERE category = '".$categories."'";
$query .= "ORDER BY `rate` DESC, `like` DESC";
问题是当结果具有相同的值时,它们按字母顺序显示.所以,我想在按速率和喜欢排序之前随机化数据库.我只是想让所有物品和具有相同价值的物品都有相同的机会,而不是按字母顺序排列.
The problem is when the results have the same value, they show up in alphabetical order. So, I would like to randomize the database before I sort it by rate and like. I just want to let the same chance to all items and those that have the same value not to be advantaged by alphabetical order.
我试过了,但没有用:
$query = "SELECT * FROM infos WHERE category = '".$categories."'";
$query .= "ORDER BY RAND(), ORDER BY `rate` DESC, `like` DESC";
有人可以帮我吗?我被卡住了.
Can somebody help me? I'm stuck.
推荐答案
你应该扭转局面:
SELECT * FROM infos WHERE category=...
ORDER BY rate DESC, like DESC, RAND();
这样就按比率排序,然后点赞,最后随机,如果比率和点赞相等.
That way it sorts by rate, then like and finally random if rate and like are equal.
此外,您使用 RAND() 的原始 ORDER BY 也不起作用,因为您使用了两个 ORDER BY 子句.
Also your original ORDER BY with RAND() doesn't work because you use two ORDER BY clauses.
这篇关于Mysql:随机排序而不是按特定列排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!