Codeigniter模型中的ORDER By

relj7zay  于 2023-09-28  发布在  其他
关注(0)|答案(3)|浏览(86)
$query = ('SELECT tb_imam.*, tb_bidang.bidang
                 FROM tb_imam JOIN tb_bidang
                   ON tb_imam.bidang_id = tb_bidang.id
                WHERE tb_imam.status = "Pengerja"
            ');

    return $this->db->query($query)->result_array();

我如何使用这样的代码添加订单?求你帮忙,谢谢

a64a0gku

a64a0gku1#

这里使用的是类似mysql query的raw query,所以可以直接在“where”子句的末尾添加“order by”子句,

$query = ('SELECT tb_imam.*, tb_bidang.bidang
             FROM tb_imam JOIN tb_bidang
               ON tb_imam.bidang_id = tb_bidang.id
            WHERE tb_imam.status = "Pengerja"
            ORDER BY column_name');

return $this->db->query($query)->result_array();
8oomwypt

8oomwypt2#

使用ORDER BY即可

$query = "SELECT tb_imam.*, tb_bidang.bidang
         FROM tb_imam 
         JOIN tb_bidang ON tb_imam.bidang_id = tb_bidang.id
         WHERE tb_imam.status = 'Pengerja' 
         ORDER BY prefix.column_name";

return $this->db->query($query)->result_array();

JOIN !== LEFT JOIN
习惯Query Builder Class

bhmjp9jg

bhmjp9jg3#

$this->db->select("tb_imam.*,tb_bidang.bidang");

$this->db->from("tb_imam");

$this->db->join("tb_bidang","tb_imam.bidang_id = tb_bidang.id");

$this->db->where("tb_imam.status","Pengerja");

$this->db->order_by("column_name");

$this->db->get()->result_array();

相关问题