gpt4 book ai didi

php - Yii:从某个表中获取最重复的ID

转载 作者:行者123 更新时间:2023-11-29 11:49:30 24 4
gpt4 key购买 nike

我是 yiibie,我正在尝试对名为 user_join_event 的表运行查询,通过该查询我可以获得该表中重复次数最多的 5 个 user_id ,这样我就可以拥有 5 个最活跃的用户 ID。查询是

SELECT `user_id`
FROM `user_join_event`
GROUP BY `user_id`
ORDER BY COUNT(*) DESC
LIMIT 5;

这个查询给了我完美的结果,我已经转换了这个查询,以便我可以在 yii 中使用它,即:

$sql = Yii::app()
->db
->createCommand()
->select('user_id')
->from('user_join_event')
->group('user_id')
->order('COUNT(*) DESC')
->limit(4)
->queryAll()

现在,我在 UserJoinEvent Controller 中创建了一个 allstats 函数,它是:

public function actionAllstats()
{
$rbmodel = UserJoinEvent::model()->findAll();
$this->layout = 'main';
$sql = Yii::app()
->db
->createCommand()
->select('user_id')
->from('user_join_event')
->group('user_id')
->order('COUNT(*) DESC')
->limit(4)
->queryAll();

$this->render('allstats', array("sql" => $sql, "rbmodel" => $rbmodel));
}

还有一个allstat.php(在UserJoinEvent的 View 文件中),它是:

<div>
<?php foreach ($rbmodel as $show): ?>
<h3><?php echo $show->user_id?></h3>
<?php endforeach; ?>
</div>

这样我就可以在此 View 页面上获得前 5 位成员(member) ID。但现在,根据此代码,它为我提供了 UserJoinEvent 表中的所有 user_id,而不是表中重复次数最多的 5 个 id。请帮我获取重复次数最多的 5 个用户的 ID。谢谢。

最佳答案

我认为您应该仅通过查询获取模型并使用它们,(在您看来,您是回显完全访问 UserJoinTAble 的结果)尝试以这种方式修改你的代码

    public function actionAllstats()
{

$this->layout='main';
$user=UserJoinEvent::model()->findAll(array(
'select'=>'user_id',
'group'=>'user_id',
'order'=>'COUNT(*) DESC',
'limit' => 4
));
$this->render('allstats', array("user"=>$user));
}


And a allstat.php(in the view file of UserJoinEvent) which is

<div>
<?php
foreach($user as $show)
{
echo '<h3>' . $show->user_id . '</h3>';
}
?>
</div>

关于php - Yii:从某个表中获取最重复的ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34372605/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com