简体   繁体   中英

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. 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 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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