字段列表中的列“名称”是不明确的codeigniter

时间:2015-02-27 12:04:04

标签: mysql codeigniter

这是我的表结构。 tbl_user: 公司名称,姓名,电子邮件。 tbl_userinfo: 电话,地址,面积,州,市,描述。 tbl_category: CATEGORY_ID,名称。 我在这里加入三张桌子是我的模特

$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));

我想要第三列名称,但我在字段列表中获得列'名称'是不明确的共同点

我使用的另一个代码,但它不会向我显示加入两个表的任何值。

$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));

3 个答案:

答案 0 :(得分:0)

这很有可能发生,因为你的两张桌子都有一个名字'柱。我建议使用[table name]。[column]明确选择列。

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

答案 1 :(得分:0)

试试这个:

 $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();

您可以使用要从该表中获取的列名替换'tbl_user.*',例如:'tbl_user.user_id, tbl_user.name as UName, tbl_category.name as CName'如果在不同的表中有2个或更多具有相同名称的列,则更改列的名称正在加入,它将始终创建一个模糊的错误,因为有两列具有相同的名称,并且codeigniter不知道您想要哪一个,如果您想要两者都需要重命名这两列。

同时将连接添加到左侧,因为如果没有user_id或category_id,它将不会显示结果。 希望这会对你有所帮助。

答案 2 :(得分:0)

带有我自己的代码。

$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();