Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/278.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 按该行的计数对结果排序_Php_Mysql_List_Sql Order By - Fatal编程技术网

Php 按该行的计数对结果排序

Php 按该行的计数对结果排序,php,mysql,list,sql-order-by,Php,Mysql,List,Sql Order By,我正在尝试创建一个函数,该函数将显示我设置的行Y的顶部X结果,在本例中,我使用了行浏览器和我的表统计数据中的顶部5结果,其中只是为了避免显示搜索机器人结果。我还希望它返回行数的计数。假设浏览器“Safari”有10个结果,那么它将返回该结果以及结果本身的计数10 $display->show_list('statistics', 'browser', '5', 'WHERE browser!=\'Search Bot\''); 这是我的功能。我对它进行了一些清理,以便在查询失败时删除某些

我正在尝试创建一个函数,该函数将显示我设置的行Y的顶部X结果,在本例中,我使用了行浏览器和我的表统计数据中的顶部5结果,其中只是为了避免显示搜索机器人结果。我还希望它返回行数的计数。假设浏览器“Safari”有10个结果,那么它将返回该结果以及结果本身的计数10

$display->show_list('statistics', 'browser', '5', 'WHERE browser!=\'Search Bot\'');
这是我的功能。我对它进行了一些清理,以便在查询失败时删除某些检查和输出,等等

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;
}

我不确定我是否理解这个问题,但是在SQL查询中使用
group by
子句怎么样

select your_column, count(*)
from your_table
where ...
group by your_column
order by count(*) desc
limit 5
这会让你:

  • 对于您的_列的每个值
  • 具有该值的行数
  • 您将保留_列中行数最多的5个值
更改此行:

$item = mysql_query("SELECT $row, count(*) as rows FROM $table $where GROUP BY count(*) ORDER BY count(*) DESC LIMIT $limit");
完成此操作后,您可能不想将其与通用的
show\u list
功能混用

随机的音符。DISTINCT在数据库上的开销往往比较大。我会避免默认做这件事

顺便说一句,你的缩进很有趣。没有一个像样的程序员会希望在不重新格式化为一致的缩进方案的情况下使用该代码。关于格式化的最佳方式,有无数的争论,但以下是对原始代码的合理缩进:

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;
}
  • 替换为
    ——我们实际上是在变量中传递列名
  • $where=null
    替换为
    $where='
    -即使
    $where
    null
    ,也能确保查询正常工作
  • 修改查询以生成预期结果(根据我对您问题的理解)
  • 重新格式化代码并正确缩进

。。。问题是?我该如何做我所描述的,因为这目前并没有输出那个特定行的出现计数,这正是我希望它做的。若$where is ever=NULL,正如您的函数定义所暗示的那个样,那个么您的SQL语句将变为空!你想数什么?例如,等于Safari的行数?@Richard aka cyberkiwi我知道,它应该是“”,因为如果它为null,SQL将是这样的:从表null LIMIT中选择不同的col_name 5是的,对不起,忘了问这个问题,但您以任何一种方式回答了它,谢谢。我使用
选择$row,COUNT(*)作为
中的amount来确定列数,这是我想要的,但是小组也提供了帮助。谢谢。是的,我同意我的缩进不是最好的,我需要改进它,我同意你的更可读。
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;
}