Welcome Guest, Not a member yet? Register   Sign In
How to make this query a little bit more readable?
#4

(03-18-2017, 11:06 AM)skunkbad Wrote:
(03-18-2017, 10:43 AM)meSmashsta Wrote:
PHP Code:
        $fname_format "CONCAT(b.last_name, ', ', b.first_name, ' ', b.middle_name) AS full_name";
        
$this->db->select("a.id, b.id AS employee_id, {$fname_format}, c.id AS machine_id, c.title AS machine, d.title AS hstatus, a.created_on, a.action_on, a.edit_on, a.problems_encountered, a.corrective_action,a.root_caused,a.remarks,a.item_replaced");
        
$this->db->from("reports AS a");
        
$this->db->join("employees AS b""b.id = a.employee_id");
        
$this->db->join("machines AS c""c.id = a.machine_id");
        
$this->db->join("hstatus AS d""d.id = a.hstatus_id");
        return 
this->db->get()->result_array(); 

Look at the select part, it's so ugly...

Trying to use CI query builder makes it worse than it has to be. Why not just use:


PHP Code:
$sql '
SELECT 
  a.id,
  b.id AS employee_id,
  CONCAT(
    b.last_name,
    '
',
    b.first_name,
    ' ',
    b.middle_name
  ) AS full_name,
  c.id AS machine_id,
  c.title AS machine,
  d.title AS hstatus,
  a.created_on,
  a.action_on,
  a.edit_on,
  a.problems_encountered,
  a.corrective_action,
  a.root_caused,
  a.remarks,
  a.item_replaced 
FROM
  reports AS a 
  LEFT JOIN employees AS b 
    ON b.id = a.employee_id 
  LEFT JOIN machines AS c 
    ON c.id = a.machine_id 
  LEFT JOIN hstatus AS d 
    ON d.id = a.hstatus_id 
'
;
$query $this->db->query($sql); 
At least then you can read it.

Thanks.
Reply


Messages In This Thread
RE: How to make this query a little bit more readable? - by meSmashsta - 03-18-2017, 05:39 PM



Theme © iAndrew 2016 - Forum software by © MyBB