简体   繁体   English

如果两个或更多列具有相同的值,如何获取具有最高值的行

[英]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.

相关问题 PHP和MYSQL - 如何检查两行或更多行是否具有相同的字段值 - PHP & MYSQL - How To Check If Two Or More Rows Have The Same Field Value SQL:如何获得具有相同值的连续行的最高计数? - SQL: Way to get highest count of consecutive rows with same value? 如果我有几行具有相同的值,如何从mysql结果中获取单个值 - How can I get a single value from a mysql result, if I have several rows with the same value SQL,如何通过已知值获取在另一列中具有相同值的行? - SQL, how to get rows by known value that have the same value in another column? 从两个表中获得最高的总和值 - Get highest sum value from two tables 选择两行具有相同列值的位置并回显它们 - Select where two rows have the same column value and echo them 选择包含的两列> 1行具有相同的值 - select the two columns contained > 1 row have the same value 当两列的值相同时,SQL会引发错误 - SQL throws an error when two columns have the same value 如何在 laravel 查询生成器中获取列具有最高值且另一列具有特定值的行 - How to get rows where a column has highest value and another column has a specific value in laravel query builder MySQL (Laravel Eloquent) - 当两列同时有两个或多个值时获取记录 - MySQL (Laravel Eloquent) - Get record when two columns have two or more values at the same time
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM