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

How do I add an order by using my code like this? beg for your help, thank you

Edwin Nick
  • 39
  • 6
  • Since you're using a raw SQL string anyway, just add it to the raw string e.g. `$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 something ');` – ADyson Jan 20 '20 at 10:42
  • Solved...!! thank you verry much,, – Edwin Nick Jan 20 '20 at 10:48

3 Answers3

1

Just use 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

Get used to Query Builder Class.

halfer
  • 19,824
  • 17
  • 99
  • 186
Abdulla Nilam
  • 36,589
  • 17
  • 64
  • 85
1

Here you are using raw query like mysql query, so you can directly add "order by" clause at the end of the "where" clause,

 $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();
Abdulla Nilam
  • 36,589
  • 17
  • 64
  • 85
Badrinath
  • 501
  • 5
  • 14
0
$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();
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • While this code may answer the question, providing additional context regarding how and/or why it solves the problem would improve the answer's long-term value. – Vickel Feb 23 '20 at 20:37