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

(03-18-2017, 03:29 PM)PaulD Wrote: If you are going to spread it out for readability, how about something like:

PHP Code:
return $this->db->from("reports AS a")
 
     ->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"
),
 
     ->join("employees AS b""b.id = a.employee_id"),
 
     ->join("machines AS c""c.id = a.machine_id"),
 
     ->join("hstatus AS d""d.id = a.hstatus_id"),
 
     ->get()
 
     ->result_array(); 

You can chain the db calls which makes everything so much neater too.

I am not sure why you have all the AS statements. Personally my tables would have columns like machine_id and employee_id and every field in, say, the employee table starts with employee_first_name, employee_last_name etc. So no collisions, and much simpler queries, and I always know what table produced what field without any backtracking through queries. Personally I hate all the AS a and a.id = b.id or c.id as it is meaningless for a human reader.

I'll take a note at that.
Reply


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



Theme © iAndrew 2016 - Forum software by © MyBB