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

发布于 2024-09-14 17:50:52 字数 701 浏览 6 评论 0原文

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

我正在尝试显示与已提出的问题相关的所有答案。我正在使用 Kohana 3。

到目前为止,我能够通过以下方式确定哪条记录具有最高的 vote_points:

$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~~~

我需要弄清楚一种确保带有 $best_id 的记录首先显示且仅显示一次的方法,而其余答案则按created_at asc 显示和排序。

谢谢大家!

Hello everyone and as usual, thank you to anyone taking the time to read this.

I am attempting to display all of the answers relevant to a question that has been asked. I am using Kohana 3.

So far, I am able to determine which record has the highest amount of vote_points via:

$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();

And, I gather all of my answers and display them by placing the result set in a foreach loop:

<?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~~~

I need to figure out a way to ensure that the record with $best_id is displayed first, and only once, while the rest of the answers are displayed and ordered by created_at asc.

Thank you, everyone!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

ゞ花落谁相伴 2024-09-21 17:50:52

此查询应该执行所需的操作:

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

This query should do the needful:

select * 
from answers where question_id = X
order by 
  (points = (select max(points) from answers where question_id = X)) desc,    
  created_at asc
无悔心 2024-09-21 17:50:52

我会这样写:(

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
}

I would write that something like:

SELECT id, count(id) as C FROM answers group by id order by C desc limit 10 

(limit 10 only if you want a specific number)

Then you can loop over the result, stuff each into an array:

$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
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文