Developer
Developer

Reputation: 39

mysql join same table with another table

I have a table name tbl_user enter image description here

This table contain a column name db_responsibleid contain the id of the responsible of this person and this person is in this table also example mohammad have db_uid=1 and samir have db_responsibleid=1 this mean that mohammad is responsible for samir

also i have another table name tbl_department the table user contain the id of the department on the column name db_udepartment

I try to have the name of the department and the name and the last name of the responsible where db_uid='$id'

$id will be getting from url

this is the query that i use

     SELECT 
     user.db_uid,
     user.db_fname,
     user.db_lname,
     user.db_username,
     user.db_pass,
     user.db_email,
     user.db_phone,
     user.db_jobtitle,
     user.db_level,
     user.db_isemployee,
     user.db_responsibleid,
     user.db_companyname,
     user.db_udepartment,
     tbl_department.db_department,
     tbl_department.db_did,
     parent.db_responsibleid,
     parent.db_fname as pfname,
     parent.db_lname as plname,
     parent.db_uid
     from tbl_user as user,tbl_department 
     join tbl_user as parent 
     on 
     user.db_responsibleid=parent.db_uid 
     where 
     user.db_udepartment=tbl_department.db_did 
     and 
     user.db_uid='$id' 

But this query give me this error Unknown column 'user.db_responsibleid' in 'on clause'

How can i solve this problem and get all information with the name of the responsible and the name of the department ??

Upvotes: 0

Views: 45

Answers (1)

Jens
Jens

Reputation: 69440

You can not mix implizit and explizit join. If you decided to use join you have to do it everywhere:

from tbl_user as user
 join tbl_department on user.db_udepartment=tbl_department.db_did 
 join tbl_user as parent 
 on 
 user.db_responsibleid=parent.db_uid 
 where 
 user.db_uid='$id' 

Hint: use prepared Statements to prevent SQL-injection

Upvotes: 1

Related Questions