In SQL Server 2008 I have two tables. One table for users:
id_user - integer, primary key
id_group - integer
username - varchar
phone - varchar
id_coordinating - int
Sample data:
1, 1, admin, 0230029921, NULL
2, 2, supervisor1, 0230029933, NULL
3, 2, supervisor2, 0321211233, NULL
4, 3, user1, 0321222323, 2
5, 3, user2, 0323211232, 2
6, 3, user3, 0324223121, 3
All users in group USERS have one supervisor represented by the id_user
And another table for groups
id_group - integer, primary key
groupname - varchar
Sample data:
1, Administrators
2, Supervisors
3, Users
I’m running the the following query:
select id_user, username, group.groupname, phone, id_coordinating
from users
INNER JOIN group ON users.id_group = group.id_group
and I get the following result:
1 1 admin Administrators 0230029921 NULL
2 2 supervisor1 Supervisors 0230029933 NULL
3 2 supervisor2 Supervisors 0321211233 NULL
4 3 user1 Users 0321222323 2
5 3 user2 Users 0323211232 2
6 3 user3 Users 0324223121 3
I want to replace the id_coordonating with the username and look like this…
1 1 admin Administrators 0230029921 NULL
2 2 supervisor1 Supervisors 0230029933 NULL
3 2 supervisor2 Supervisors 0321211233 NULL
4 3 user1 Users 0321222323 supervisor1
5 3 user2 Users 0323211232 supervisor1
6 3 user3 Users 0324223121 supervisor2
Thanks,
You can
left joinback onto your users table for this.