简体   繁体   English

字段列表中的列“ student_id”不明确

[英]Column 'student_id' in field list is ambiguous

have a function get_autocomplete in model search 在模型搜索中具有功能get_autocomplete

public function get_autocomplete()
    {
        $x = array();
        $search_data = "sah"; 
        $this->db->select('student_id,filename,random,type,mime');
        $this->db->from('uploads a');
        $this->db->or_like('filename',$search_data);
        $this->db->or_like('random',$search_data);
        $this->db->or_like('type',$search_data);
        $this->db->or_like('mime',$search_data);
        $this->db->join('student-record c', 'c.student_id=a.student_id', 'left');
        $this->db->like('name',$search_data);
        $this->db->or_like('email',$search_data);
        $this->db->or_like('phone',$search_data);
        $res   = $this->db->get();
    }

but I get error which states 但我得到哪个错误

Error Number: 1052 错误编号:1052

Column 'student_id' in field list is ambiguous 字段列表中的列“ student_id”不明确

SELECT student_id , filename , random , type , mime FROM ( uploads a) LEFT JOIN student-record c ON c . SELECT student_idfilenamerandomtypemime FROM( uploads a)LEFT JOIN student-record c ON c student_id = a . student_id = a student_id WHERE filename LIKE '%sah%' OR random LIKE '%sah%' OR type LIKE '%sah%' OR mime LIKE '%sah%' AND name LIKE '%sah%' OR email LIKE '%sah%' OR phone LIKE '%sah%' student_id WHERE filename像'%sah%'或random像'%sah%'或type像'%sah%'或mime像'%sah%'并name像'%sah%'或email像'%sah%'或phone喜欢'%sah%'

Filename: C:\\wamp\\www\\ededge2\\system\\database\\DB_driver.php 档案名称:C:\\ wamp \\ www \\ ededge2 \\ system \\ database \\ DB_driver.php

Line Number: 330 行号:330

Help me to recover this. 帮我找回这个。

That's because both the tables (uploads,student-record) have student_id key in it. 这是因为两个tables (上载,学生记录)中都有student_id键。

When you use JOIN make sure you use alias to access the particular column in case of the both tables have similar column names. 当使用JOIN ,如果两个表的列名相似,请确保使用别名来访问特定的列。

So you can update your query like this, 因此,您可以像这样更新查询,

public function get_autocomplete()
    {
        $x = array();
         $search_data = "sah"; 
        $this->db->select('a.student_id,filename,random,type,mime');
        $this->db->from('uploads a');
        $this->db->or_like('filename',$search_data);
        $this->db->or_like('random',$search_data);
        $this->db->or_like('type',$search_data);
        $this->db->or_like('mime',$search_data);
        $this->db->join('student-record c', 'c.student_id=a.student_id', 'left');
        $this->db->like('name',$search_data);
        $this->db->or_like('email',$search_data);
        $this->db->or_like('phone',$search_data);
        $res   = $this->db->get();
    }

Column 'student_id' in field list is ambiguous, 字段列表中的列“ student_id”不明确,
It means it doesn't know which column value it has to return, either from uploads or student-record . 这意味着无论是上传还是学生记录 ,它都不知道必须返回哪个列值。


Update: 更新:

$this->db->select('a.student_id,filename,random,type,mime,name,phone');
// add name and phone to get it in the result-set.

使用相同的字段名称两个表联接时,会发生此问题。要解决此问题,在获取数据“ table_name.student_id作为stu_id”时应使用此方法。您的问题将得到解决,并且您可以通过stu_id访问student_id。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM