简体   繁体   English

按行数计算结果

[英]Order results by count of that row

I'm trying to make a function which will display the top X results of the row Y which I set, for this instance I'm using the row browser and the top 5 results from my table statistics , the where is just to eliminate Search Bot results from showing up. 我正在尝试创建一个函数,它将显示我设置的行Y的前X个结果,对于此实例,我使用行浏览器和我的表统计信息中的前5个结果,其中只是为了消除搜索出现Bot结果。 I also want it to return the count of the amount of rows as well. 我也希望它返回行数的计数。 So say there was 10 results for the browser 'Safari', then it would return the count of 10 for that result as well as the result itself. 所以说浏览器'Safari'有10个结果,那么它将返回该结果的10个数以及结果本身。

$display->show_list('statistics', 'browser', '5', 'WHERE browser!=\'Search Bot\'');

Here is my function. 这是我的功能。 I'm cleaned it up a bit to remove certain checks and outputs if the query were to fail, etc. 如果查询失败,我会清理一下以删除某些检查和输出等。

function show_list($table, $row, $limit = 5, $where = NULL) {

$item = mysql_query("SELECT DISTINCT $row FROM $table $where LIMIT $limit");

            $result = array();

while ($fetch = mysql_fetch_array($item)) {

            $result[] = $fetch;
}
            return $result;
}

Not sure I understand the question, but what about using a group by clause, in your SQL query : 我不确定我理解这个问题,但是在SQL查询中使用group by子句会怎么样:

select your_column, count(*)
from your_table
where ...
group by your_column
order by count(*) desc
limit 5

That would get you : 那会得到你:

  • for each value of your_column , 对于your_column每个值,
  • the number of rows with that value 具有该值的行数
  • and you'd keep the 5 values of your_column that have the biggest number of rows 并且你将保留your_column的5个值, your_column值具有最大的行数

Change this line: 改变这一行:

$item = mysql_query("SELECT $row, count(*) as rows FROM $table $where GROUP BY count(*) ORDER BY count(*) DESC LIMIT $limit");

After doing that, you may not want to mix this with your generic show_list function. 执行此操作后,您可能不希望将其与通用show_list函数混合使用。

A random note. 一个随机的说明。 DISTINCT tends to be somewhat expensive on the database. DISTINCT在数据库上往往有些昂贵。 I would avoid defaulting to doing it. 我会避免默认这样做。

Incidentally your indentation is..interesting. 顺便说一句,你的缩进是有趣的。 No decent programmer will want to work with that code without reformatting to some consistent indentation scheme. 没有合适的程序员会想要使用该代码而无需重新格式化为某些一致的缩进方案。 There are endless arguments about the best way to format, but the following would be a reasonable indentation of your original code: 关于格式化的最佳方法存在无穷无尽的争论,但以下是对原始代码的合理缩进:

function show_list($table, $row, $limit = 5, $where = NULL) {

    $item = mysql_query("SELECT DISTINCT $row FROM $table $where LIMIT $limit");

    $result = array();

    while ($fetch = mysql_fetch_array($item)) {    
        $result[] = $fetch;
    }
    return $result;
}
function show_list($table, $column, $limit = 5, $where = '') {
    $item = mysql_query("SELECT $column, COUNT(*) AS c FROM $table $where
            GROUP BY $column ORDER BY c LIMIT $limit");
    $result = array();
    while ($fetch = mysql_fetch_array($item)) {
        $result[] = $fetch;
    }
    return $result;
}
  • Replace row with column - we are actually passing a column name in the variable. 更换rowcolumn -我们实际上是在变量传递列名。
  • Replace $where = null with $where = '' - it will ensure that the query works correctly even when $where is null . $where = ''替换$where = null - 即使$wherenull ,它也将确保查询正常工作。
  • Modify the query to generate the expected results (to the best of my understanding of your question) 修改查询以生成预期结果(根据我对您的问题的理解)
  • Re-format the code and indent it properly. 重新格式化代码并正确缩进。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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