gpt4 book ai didi

mysql - Codeigniter:无法按降序显示结果和计数

转载 作者:行者123 更新时间:2023-11-30 01:38:06 25 4
gpt4 key购买 nike

我有两张 table ,

instructions 
-------------
instruction_id int(11) Primary key (Auto_increment)
instruction text
user_id int(11) Foreign key


instrunctionlike
-----------------
instrunction_likeid int(11) Primary key (Auto_increment)
instrunction_id int(11) Foreign Key

在指令和类指令表中有很多行,我想要的是,从类似指令的表中按降序顺序获取喜欢计数。 例如。 select*from instructionlike order by count(instructionlike.instruction_likeid)...

下面是我尝试过的,但我很困惑如何使用 desc 顺序实现行计数。请帮我解决这个问题

//-----------------------------------------------------------------------------------
public function fetch_agreed_desc_order_instruction($limit, $start) {
$this->load->database();
$this->db->limit($limit, $start);
$this->db->join('userdetails', 'userdetails.user_id = instructions.user_id');
$this->db->join('instrunctionlike', 'instrunctionlike.instrunction_id = instructions.instruction_id');
$this->db->order_by('instrunctionlike.instrunction_id', 'DESC');
$this->db->group_by("instrunctionlike.instrunction_id");
$query = $this->db->get("instructions");
if ($query->num_rows() > 0) {
foreach ($query->result() as $row) {
$data[] = $row;
}
return $data;
}
return false;
}

示例输出:

             instructions                Likes
............. 78
............. 66
............. 56
............. 34
............. 12
............. 1
............. 1
............. 0

最佳答案

虽然您确实想按 instruction_id 进行分组,但您不想按该顺序进行排序。您想按 COUNT(instruction_id) 订购:

$this->db->order_by('COUNT(instrunctionlike.instrunction_id)', 'DESC');
$this->db->group_by("instrunctionlike.instrunction_id");

关于mysql - Codeigniter:无法按降序显示结果和计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16558432/

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