[英]How to get rows with highest value, if two or more Columns have same value
I have posts table, i want to display most viewed posts first, but i am not getting proper result if two or more columns have same value. 我有帖子表,我想首先显示查看最多的帖子,但是如果两个或多个列具有相同的值,我将无法获得正确的结果。 I mean if one posts have 25 views and second have the same(25) views, then i am getting only one result, but i want to display all the results.
我的意思是,如果一个帖子具有25个视图,第二个帖子具有相同的(25)视图,那么我只会得到一个结果,但是我想显示所有结果。 Help me please..
请帮帮我..
Here is My Code : 这是我的代码:
$real_query = mysqli_query(
$connect,
"SELECT id, note_topic, note_owner_id, note_by_name,
views FROM user_note GROUP BY views ORDER BY MAX(views) DESC"
);
Just remove grouping by views: 只需删除按视图分组:
GROUP BY views
Also remove aggregation from ORDER BY clause: 还要从ORDER BY子句中删除聚合:
ORDER BY MAX(views) DESC
to 至
ORDER BY views DESC
I think you are looking for a query like this one: 我认为您正在寻找这样的查询:
select
id, note_topic, note_owner_id, note_by_name, views
from
user_note
where
views = (select MAX(views) from user_note)
if there's only one maximum post, this query will just return 1 row, otherwise it will return all rows that have the maximum number of views. 如果只有一个最大帖子,则此查询将仅返回1行,否则将返回所有具有最大视图数的行。
This will display the top ten records ordered by the number of views, in descendant order, 这将显示按视图数顺序排列的前十个记录,顺序为后代,
including records that have the same number of top views 包括具有相同顶视图数量的记录
SELECT
id, note_topic, note_owner_id, note_by_name, views
FROM
user_note
ORDER BY views DESC
LIMIT 10
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.