【问题标题】:How to query the database to display the most data only?如何查询数据库只显示最多的数据?
【发布时间】:2017-08-06 11:23:49
【问题描述】:

我有这样的桌子

Table

我只想根据出现在第二个字段中的最多数据来显示数据,我不想显示很少的数据。所以数据只出现最多,并在第三个字段中计算总值。而且我试过了,但没有结果。 如何查询数据库只显示最多的数据

【问题讨论】:

    标签: mysql database


    【解决方案1】:

    你可以这样做,

    SELECT       `col_name_of_frequent_value`,
             COUNT(`col_name_of_frequent_value`) AS `frequent_value` 
    FROM     `table_name`
    GROUP BY `col_name_of_frequent_value`
    ORDER BY `frequent_value` DESC // This will sort the result by putting max count at top.
    LIMIT    1; // This will only show the TOP-Most value in sorted result.
    

    请根据您的表结构更改名称。

    【讨论】:

      【解决方案2】:
      SELECT 
      kd_masalah, total_bobot
      FROM 
      (SELECT                            -- SubQuery
          SUM(bobot) AS total_bobot,     -- Gives the sum of values in bobot column  relating to a particular group (grouped column)
          kd_masalah 
      FROM
          your_table_name
      GROUP BY kd_masalah                -- Grouping of same values in the column
      )AS s having MAX(total_bobot);     -- return the values having max sum in total_bobot
      

      【讨论】:

      • 虽然只有代码的答案可能是正确的,但最好添加一些解释,说明此代码如何或为何成为解决方案。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-19
      • 2015-09-15
      • 2020-05-27
      相关资源
      最近更新 更多