1

I need to join two tables but both the table has a field named 'username' both the table has different values i, i need to display both values in the field 'username' how it possible in codeigniter

public function tech_work_completed($id) {
    $this->db->select('c.username,u.id,tw.*,w.*');
    $this->db->from('tbl_tech_work tw');
    $this->db->join('tbl_customers c', 'tw.user_id = c.id', 'left');
    $this->db->join('tbl_users u', 'u.id = tw.technician_id', 'left');
    $this->db->join('tbl_works w', 'w.user_id = c.id', 'left');
    $this->db->where('w.wrk_status', 'completed');
    $query = $this->db->get();
    return $query->result();
}

this is my model , both table tbl_customer and tbl_users contain a field name username so when display username it get from tbl_user. I need from tbl_customers

1
  • Can you show us the contents of $query->result()? It'll be easier to visualise. Commented Nov 24, 2016 at 9:35

2 Answers 2

4

Use column aliasing at the time of column selection like:

t1.username as uname1, t2.username as uname2

By using this you will two column with name uname1, uname2

or

$this->db->select('course_name AS `Course Name`, course_desc AS `Course Description`, display_public AS `Display Status`', FALSE);
Sign up to request clarification or add additional context in comments.

Comments

0

In addition to Mayank Pandeyz's answer:

Since you want to display both values in the field 'username', along with the alias, you can use CONCAT.

After you've set the aliases t1.username as uname1, t2.username as uname2 as Mayank Pandeyz described, you can "make" the field username like so:

concat( t1.username, " ",t2.username ) AS username

This will create new column in the result, called as you want (in this case "username"). It will contain both values, separated by whitespace (separator is chosen by us).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.