Fetch data from same named two columns of two tables

Issue

I am trying to fetch data from 2 tables with a join query. Here I have 2 columns in 2 tables with same column name.

This is my query:

public function get_all_expenses()
{
        $this->db->select("*",'category.name as cat_name');
        $this->db->from('expense');
        $this->db->join('category','expense.cat_id = category.id');
        $this->db->join('users','expense.user_id = users.id');
        $query = $this->db->get();

        return $query;
}

I can fetch data of 1 column of 1 table. But I can’t fetch data of another column of another table. I am using CodeIgniter.

enter image description here

Solution

According to the CodeIgniter documentation the database select method accept a single argument. The correct syntax for the select is then:

$this->db->select('*, category.name as cat_name');

Answered By – adeys

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published