Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/59.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/Kohana 3_Php_Mysql_Database_Kohana 3 - Fatal编程技术网

如何选择投票点最多的数据库记录并确保它首先显示?PHP/Kohana 3

如何选择投票点最多的数据库记录并确保它首先显示?PHP/Kohana 3,php,mysql,database,kohana-3,Php,Mysql,Database,Kohana 3,大家好,像往常一样,感谢所有花时间阅读本文的人 我试图显示与所问问题相关的所有答案。我用的是Kohana3 到目前为止,我可以通过以下方式确定哪项记录的投票点数最高: $best_id = DB::query(Database::SELECT, 'SELECT id FROM answers WHERE vote_points=(SELECT MAX(vote_points) FROM answers) AND question_id ='.$question->id)->execu

大家好,像往常一样,感谢所有花时间阅读本文的人

我试图显示与所问问题相关的所有答案。我用的是Kohana3

到目前为止,我可以通过以下方式确定哪项记录的投票点数最高:

$best_id = DB::query(Database::SELECT, 'SELECT id FROM answers WHERE vote_points=(SELECT MAX(vote_points) FROM answers) AND question_id ='.$question->id)->execute();
然后,我收集所有答案,并通过将结果集放置在foreach循环中来显示它们:

<?php foreach($question->answers->where('moderated', '=', 0)->where('deleted', '=', 0)->order_by('created_at', 'ASC')->find_all() as $answer): ?> 
A bunch of display answer functions and divs~~~

一组显示应答函数和div~~~
我需要找出一种方法来确保带有$best_id的记录首先显示,并且只显示一次,而其余答案则由asc的created_显示并排序


谢谢大家!

我会这样写:

SELECT id, count(id) as C FROM answers group by id order by C desc limit 10 
(仅当需要特定数字时,才限制为10)

然后可以循环结果,将每个结果填充到一个数组中:

$all = array ();
while ( $rs = mysql_fetch_array ( $r, MYSQL_ASSOC ) )
{
    $all[$rs['id']] = $rs['C'];
}

$best = array_shift($all);

// echo the best one here

// loop over the rest

foreach ( $all as $id => $count )
{
    // display code here
}

此查询应执行必要的操作:

select * 
from answers where question_id = X
order by 
  (points = (select max(points) from answers where question_id = X)) desc,    
  created_at asc