Friday 15 June 2012

sql - Select all Users related to Company -


First of all, sorry for the meaningless title. Please do not think anything better, please take a look at my following tables:

User

  + ----- + ------- + ------- -------- + | ID | Name | Email | + ----- + ------- + --------------- + | 1 | Name A | Namea@srv.com | | 2 | Name b Nameb@srv.com | | 3 | NameC | Namec@srv.com | + ----- + ------- + --------------- +   

Department

  + ----- + --------- + ------- + --------- + | ID | Company | Name | Manager. + ----- + --------- + ------- + --------- + | 1 | 1 | DeptA | 1 | | 2 | 1 | DeptB | 2 | + ----- + --------- + ------- + --------- +   

Company

  + ----- + ------ + ------- + | ID | Name | Owner | + ----- + ------ + ------- + | 1 | Buzz | 3 | + ----- + ------- + ------ +   

I need to find all the users related to a company. Something like this:

  + --------- + ------------ + | User_id | Company_id | + --------- + ------------ + | 1 | 1 | | 2 | 1 | | 3 | 1 | + --------- + ------------ +   

How can I do this? I have read about different types of joints (internal, external, complete, etc.), but I did not know how to handle the "department table" in the middle of everything.

I will do it with two questions:

  select the department Please. As user_id Select department.com = company.ind union company. Company_ID as the company company, company    

No comments:

Post a Comment