douwendu2460 2016-06-27 07:22
浏览 236
已采纳

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

have a function get_autocomplete in model search

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

Column 'student_id' in field list is ambiguous

SELECT student_id, filename, random, type, mime FROM (uploads a) LEFT JOIN student-record c ON c.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%'

Filename: C:\wamp\www\ededge2\system\database\DB_driver.php

Line Number: 330

Help me to recover this.

  • 写回答

2条回答 默认 最新

  • dongyu4863 2016-06-27 07:25
    关注

    That's because both the tables(uploads,student-record) have student_id key in it.

    When you use JOIN make sure you use alias to access the particular column in case of the both tables have similar column names.

    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,
    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.
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改