如何从表中获取所有数据,包括表 ID

Posted

技术标签:

【中文标题】如何从表中获取所有数据,包括表 ID【英文标题】:How to fetch all the data from a table including table Id 【发布时间】:2014-04-08 05:50:43 【问题描述】:

我使用 Join 编写了以下查询以从不同的表中获取数据。这个查询在 mysql 上运行良好,它从数据库中提取所有数据,但是当我用 php 编写这个查询时,它会提取除StudentId 之外的所有数据。当我使用print_r 时,它显示studentId 列为空。 请指导我在这里缺少什么?

型号

public function student_profile($id)

    $this->db->select('*');
    $this->db->from('student');
    $this->db->join('student_marks',                'student.studentId= student_marks.studentId','left');
    $this->db->join('sports',                       'student.studentId= sports.studentId','left');
    $this->db->join('extra_curricular_activities',  'student.studentId= extra_curricular_activities.studentId','left'   );
    $this->db->where('student.studentId', $id);

    $query=     $this->db->get();

    return $query->result();


    

控制器

        if ($user_type=='Student')  
        
            if ($LoginData= $this->loginmodel->studentLogin($username,$password))
            
                foreach($LoginData as $UserId)
                
                    $currentId= $UserId->StudentId;                         
                

                $data['students_data']=         $this->loginmodel->student_profile($currentId);

                $this->load->view('students',$data);

输出

Array
(
    [0] => stdClass Object
        (
            [StudentId] => 
            [StudentName] => Ahmed
            [UserId] => 57502
            [Password] => 28101
            [Batch] => 2009
            [Email] => ahmed@Hotmail.com
            [PhoneNumber] => 02134663028
            [DegreeId] => 1
            [in_semester] => 1
            [StudentMarksId] => 15
            [SemisterCourseId] => 1
            [AssignmentMarks] => 9
            [QuizMarks] => 9
            [ClassTestMarks] => 9
            [FinalMarks] => 45
            [Grades] => F
            [FYPMarks] => 9
            [Attendence] => 
            [GPA] => 0
            [OLQ] => 9
            [InstructorComments] => Satisfactory !!
            [InstructorRatings] => 9
            [Result] => Failed
            [Active] => No
            [SportsId] => 
            [Squash] => 
            [FootBall] => 
            [Cricket] => 
            [Hocky] => 
            [Swimming] => 
            [ExtraActivitiesId] => 
            [DebateCompetition] => 
            [QuizCompetition] => 
            [QiratCompetition] => 
            [DrillCompetition] => 
            [Others] => 
        )

    [1] => stdClass Object
        (
            [StudentId] => 
            [StudentName] => Ahmed
            [UserId] => 57502
            [Password] => 28101
            [Batch] => 2009
            [Email] => ahmed@Hotmail.com
            [PhoneNumber] => 02134663028
            [DegreeId] => 1
            [in_semester] => 1
            [StudentMarksId] => 16
            [SemisterCourseId] => 2
            [AssignmentMarks] => 2
            [QuizMarks] => 2
            [ClassTestMarks] => 2
            [FinalMarks] => 10
            [Grades] => F
            [FYPMarks] => 2
            [Attendence] => 
            [GPA] => 0
            [OLQ] => 2
            [InstructorComments] => 
            [InstructorRatings] => 0
            [Result] => Failed
            [Active] => No
            [SportsId] => 
            [Squash] => 
            [FootBall] => 
            [Cricket] => 
            [Hocky] => 
            [Swimming] => 
            [ExtraActivitiesId] => 
            [DebateCompetition] => 
            [QuizCompetition] => 
            [QiratCompetition] => 
            [DrillCompetition] => 
            [Others] => 
        )

【问题讨论】:

使用 $this->db->last_query() 打印您的查询并在数据库中手动执行它,看看您在结果中得到了什么 【参考方案1】:

因为您在表上使用LEFT 连接,并且它们为studentId 共享相同的列名,所以从 mysql 中,最后一列是从您的任何连接表中选择的,并且它具有 null 您应该为查询使用别名,并为具有相同名称的列提供新的别名,如下所示s.studentId AS student_id

function student_profile($id)

    $this->db->select('*,s.studentId AS student_id');
    $this->db->from('student s');
    $this->db->join('student_marks sm', 's.studentId= sm.studentId', 'left');
    $this->db->join('sports sp', 's.studentId= sp.studentId', 'left');
    $this->db->join('extra_curricular_activities e', 's.studentId= e.studentId', 'left');
    $this->db->where('s.studentId', $id);
    $query = $this->db->get();
    return $query->result();

当获取结果时,您在[student_id] 上拥有正确的学生 ID, 其他方法只需选择只需要的列不要选择*

【讨论】:

感谢您的帮助 :) 此代码没有给我以下错误消息 Error Number: 1054 Unknown column 'student.studentId' in 'where clause' SELECT *, s.studentId` AS student_id FROM (student s) LEFT JOIN student_marks sm ON s 987654332 sm 987654334 studentId 987654335 sports 987654336 studentId 987654338 studentId 987654340 s 987654341 s 987654341 s 987654341 @ 987654341 = e.studentId WHERE student.studentId = '41'` 查看我更新的答案将 where 子句更改为 $this->db->where('s.studentId', $id);

以上是关于如何从表中获取所有数据,包括表 ID的主要内容,如果未能解决你的问题,请参考以下文章

从表中获取全部内容

MySQL 从表中获取数据并检查表行中是不是存在用户 ID

Laravel 根据数据透视表从表中获取标题

如果值存在于多个表中,则从表中获取结果

如何获取表中的所有数据,如果在一列中重复相同的值,该行应该计数一次?

如何从表中获取数据并将其插入另一个表?