简体   繁体   中英

Select and join the same table in Codeigniter

I have stuck somewhere in the select query in CodeIgniter. I want to perform a select query with several joins but the problem is how to join the same table.

Some of My Tables

Users

id first_name middle_name last_name gender etc.

1     John        G         Doe        M

2     Marry       H         Moe        F 

Students

student_id, class, transport, parent etc

1             2       4        2

My probem is how to show parent name in my select istead of the parent id 2?

My query is this

 $sql="SELECT users.id, first_name, last_name, middle_name, 
                         email, gender, avatar, address, contact, class_name, stream_name, 
                         dormitory_name, route_name, roll_num
                         FROM users, meta_data, role_assignment, students, class, stream,
                         dormitory, transport
                         WHERE users.id=meta_data.user_id 
                         AND users.id=role_assignment.user_id  
                         AND role_assignment.role='3'
                         AND users.id=students.student_id
                         AND students.class=class.class_id
                         AND students.stream=stream.stream_id
                         AND students.dormitory= dormitory.dormitory_id
                         AND students.transport=transport.transport_id";

Any help will be appriciated.

Use this query

SELECT U.first_name, U.last_name, U.middle_name FROM users AS U LEFT JOIN Students AS S ON U.id=S.class

It will helps you to solve the problem

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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