字段列表中的列 'name' 不明确 codeigniter

Column 'name' in field list is ambiguous codigniter

这是我的 table 结构。 tbl_user: 公司名称,姓名,电子邮件。 tbl_user信息: phone,地址,地区,州,城市,描述。 tbl_category: category_id,姓名。 我加入了三个 table 这是我的模特

$this->db->select(array('name', 'companyname','phone','address','email','state','city','pincode','area','description','image', 'c.name AS categoryname'));
        $this->db->from('tbl_user');
        $this->db->join('tbl_userinfo', 'tbl_userinfo.user_id = tbl_user.user_id');
        $this->db->join ('tbl_category', 'tbl_category.category_id = tbl_userinfo.service_category');
        $this->db->group_by(array('name', 'companyname', 'phone', 'address', 'email','state','city','pincode','area','description','image','categoryname')); 
        //$this->db->order_by('tbl_category.category_id');
        $this->db->where(array('tbl_user.user_id' => 16));

我也想要第三列名称,但我在字段列表中得到的列 'name' 是不明确的代码

我确实使用了另一个代码,但它不会显示任何连接两个 table 的值。

$this->db->select(array('tbl_user.name', 'tbl_user.companyname','tbl_userinfo.phone','tbl_userinfo.address','tbl_user.email','tbl_userinfo.state','tbl_userinfo.city','tbl_userinfo.pincode','tbl_userinfo.area','tbl_userinfo.description','tbl_userinfo.image', 'tbl_category.name'));
        $this->db->from('tbl_user');
        $this->db->join('tbl_userinfo', 'tbl_userinfo.user_id = tbl_user.user_id');
        $this->db->join ('tbl_category', 'tbl_userinfo.service_category = tbl_category.category_id');
        $this->db->group_by(array('tbl_user.name', 'tbl_user.companyname','tbl_userinfo.phone','tbl_userinfo.address','tbl_user.email','tbl_userinfo.state','tbl_userinfo.city','tbl_userinfo.pincode','tbl_userinfo.area','tbl_userinfo.description','tbl_userinfo.image', 'tbl_category.name'));
        $this->db->where(array('tbl_user.user_id' => 16));

这很可能发生,因为您的两个 table 都有一个 'name' 列。我建议使用 [table name].[column].

明确选择列
$result = $this->db->query('past your query here after checking it in an sql browser');

试试这个:

 $user_id = '16';
 $query = $this->db->select('tbl_user.companyname, tbl_user.name as UserName, tbl_user.email, tbl_userinfo.phone, tbl_userinfo.address, tbl_userinfo.area, tbl_userinfo.state, tbl_userinfo.city, tbl_userinfo.description, tbl_category.category_id, tbl_category.name as CategoryName')
                    ->from('tbl_user')
                    ->join('tbl_userinfo', 'tbl_userinfo.user_id = tbl_user.user_id')
                    ->join ('tbl_category', 'tbl_category.category_id = tbl_userinfo.service_category')
                    ->where('tbl_user.user_id', $user_id)
                    ->group_by('tbl_user.companyname')
                    ->get()
                    ->result();

您可以用您想要从 table 中获取的列名重新填充 'tbl_user.*',例如:'tbl_user.user_id, tbl_user.name as UName, tbl_category.name as CName' 如果您有 2 个或更多列,请更改列名在您加入的不同 table 中有相同的名称,它总是会产生模棱两可的错误,因为有 2 列具有相同的名称,而 codeigniter 不知道您想要哪一个,如果您想要两个,则需要重命名两列。

还要将连接添加到左侧,因为如果没有 user_id 或 category_id,它将不会向您显示结果。 希望对您有所帮助。

附带我自己的代码。

$this->db->select(array('u.name', 'u.companyname','i.phone','i.address','u.email','i.state','i.city','i.pincode','i.area','i.description','i.image', 'c.name AS categoryname'));
        $this->db->from('tbl_user as u');
        $this->db->join('tbl_userinfo as i', 'i.user_id = i.user_id');
        $this->db->join ('tbl_category as c', 'c.category_id = i.service_category');
        $this->db->group_by(array('u.user_id')); 
        $this->db->where(array('u.group_id' => 16));        
        $query = $this->db->get ();
        return $query->result();