postgresql inner join and left join
up vote
1
down vote
favorite
I have the following database structure, with postgres 9.5:
create table users (
id character varying(255),
email character varying(127)
);
create table org_users(
id character varying(255),
user_id character varying(255), -- foreign key to users.id
manager_org_user_id character varying(255) -- refers to org_users.id; same table referential dependency
);
insert into users (id, email) values ('111', 'user1@example.com');
insert into users (id, email) values ('222', 'user2@example.com');
insert into users (id, email) values ('333', 'manager@example.com');
insert into org_users (id, user_id, manager_org_user_id) values ('o1', '111', 'o3');
insert into org_users (id, user_id, manager_org_user_id) values ('o2', '222', 'o3');
insert into org_users (id, user_id, manager_org_user_id) values ('o3', '333', null);
I have three users in my table. user1@example.com, user2@example.com are two normal users. manager@example.com is their manager. Now I want to display the following structure:
org_user_id, employee_user_id, employee_email, manager_org_user_id, manager_user_id, manager_email
I tried with the following sql query but it fails to work for me with syntax error near JOIN. Any help ?
select ou.id as employee_org_user_id,
us.id as employee_user_id,
us.email as employee_email,
ou.manager_org_user_id as manager_org_user_id,
u2.email as manager_email
from org_users ou
JOIN users us ON ou.user_id::text = us.id::text,
LEFT JOIN org_users ou1 ON ou1.id::text = ou.manager_org_user_id::text
LEFT JOIN users us2 ON ou1.user_id::text = us2.id::text;
sql database postgresql join
add a comment |
up vote
1
down vote
favorite
I have the following database structure, with postgres 9.5:
create table users (
id character varying(255),
email character varying(127)
);
create table org_users(
id character varying(255),
user_id character varying(255), -- foreign key to users.id
manager_org_user_id character varying(255) -- refers to org_users.id; same table referential dependency
);
insert into users (id, email) values ('111', 'user1@example.com');
insert into users (id, email) values ('222', 'user2@example.com');
insert into users (id, email) values ('333', 'manager@example.com');
insert into org_users (id, user_id, manager_org_user_id) values ('o1', '111', 'o3');
insert into org_users (id, user_id, manager_org_user_id) values ('o2', '222', 'o3');
insert into org_users (id, user_id, manager_org_user_id) values ('o3', '333', null);
I have three users in my table. user1@example.com, user2@example.com are two normal users. manager@example.com is their manager. Now I want to display the following structure:
org_user_id, employee_user_id, employee_email, manager_org_user_id, manager_user_id, manager_email
I tried with the following sql query but it fails to work for me with syntax error near JOIN. Any help ?
select ou.id as employee_org_user_id,
us.id as employee_user_id,
us.email as employee_email,
ou.manager_org_user_id as manager_org_user_id,
u2.email as manager_email
from org_users ou
JOIN users us ON ou.user_id::text = us.id::text,
LEFT JOIN org_users ou1 ON ou1.id::text = ou.manager_org_user_id::text
LEFT JOIN users us2 ON ou1.user_id::text = us2.id::text;
sql database postgresql join
1
JOIN users us ON ou.user_id::text = us.id::text,you need to remove,at end; alsou2.email as manager_emailshould beus2.email; no need for cast to text (though it doesn't hurt output)
– user8834780
Nov 13 at 16:58
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I have the following database structure, with postgres 9.5:
create table users (
id character varying(255),
email character varying(127)
);
create table org_users(
id character varying(255),
user_id character varying(255), -- foreign key to users.id
manager_org_user_id character varying(255) -- refers to org_users.id; same table referential dependency
);
insert into users (id, email) values ('111', 'user1@example.com');
insert into users (id, email) values ('222', 'user2@example.com');
insert into users (id, email) values ('333', 'manager@example.com');
insert into org_users (id, user_id, manager_org_user_id) values ('o1', '111', 'o3');
insert into org_users (id, user_id, manager_org_user_id) values ('o2', '222', 'o3');
insert into org_users (id, user_id, manager_org_user_id) values ('o3', '333', null);
I have three users in my table. user1@example.com, user2@example.com are two normal users. manager@example.com is their manager. Now I want to display the following structure:
org_user_id, employee_user_id, employee_email, manager_org_user_id, manager_user_id, manager_email
I tried with the following sql query but it fails to work for me with syntax error near JOIN. Any help ?
select ou.id as employee_org_user_id,
us.id as employee_user_id,
us.email as employee_email,
ou.manager_org_user_id as manager_org_user_id,
u2.email as manager_email
from org_users ou
JOIN users us ON ou.user_id::text = us.id::text,
LEFT JOIN org_users ou1 ON ou1.id::text = ou.manager_org_user_id::text
LEFT JOIN users us2 ON ou1.user_id::text = us2.id::text;
sql database postgresql join
I have the following database structure, with postgres 9.5:
create table users (
id character varying(255),
email character varying(127)
);
create table org_users(
id character varying(255),
user_id character varying(255), -- foreign key to users.id
manager_org_user_id character varying(255) -- refers to org_users.id; same table referential dependency
);
insert into users (id, email) values ('111', 'user1@example.com');
insert into users (id, email) values ('222', 'user2@example.com');
insert into users (id, email) values ('333', 'manager@example.com');
insert into org_users (id, user_id, manager_org_user_id) values ('o1', '111', 'o3');
insert into org_users (id, user_id, manager_org_user_id) values ('o2', '222', 'o3');
insert into org_users (id, user_id, manager_org_user_id) values ('o3', '333', null);
I have three users in my table. user1@example.com, user2@example.com are two normal users. manager@example.com is their manager. Now I want to display the following structure:
org_user_id, employee_user_id, employee_email, manager_org_user_id, manager_user_id, manager_email
I tried with the following sql query but it fails to work for me with syntax error near JOIN. Any help ?
select ou.id as employee_org_user_id,
us.id as employee_user_id,
us.email as employee_email,
ou.manager_org_user_id as manager_org_user_id,
u2.email as manager_email
from org_users ou
JOIN users us ON ou.user_id::text = us.id::text,
LEFT JOIN org_users ou1 ON ou1.id::text = ou.manager_org_user_id::text
LEFT JOIN users us2 ON ou1.user_id::text = us2.id::text;
sql database postgresql join
sql database postgresql join
edited Nov 13 at 16:57
asked Nov 13 at 16:51
Sankar
2,20283253
2,20283253
1
JOIN users us ON ou.user_id::text = us.id::text,you need to remove,at end; alsou2.email as manager_emailshould beus2.email; no need for cast to text (though it doesn't hurt output)
– user8834780
Nov 13 at 16:58
add a comment |
1
JOIN users us ON ou.user_id::text = us.id::text,you need to remove,at end; alsou2.email as manager_emailshould beus2.email; no need for cast to text (though it doesn't hurt output)
– user8834780
Nov 13 at 16:58
1
1
JOIN users us ON ou.user_id::text = us.id::text, you need to remove , at end; also u2.email as manager_email should be us2.email; no need for cast to text (though it doesn't hurt output)– user8834780
Nov 13 at 16:58
JOIN users us ON ou.user_id::text = us.id::text, you need to remove , at end; also u2.email as manager_email should be us2.email; no need for cast to text (though it doesn't hurt output)– user8834780
Nov 13 at 16:58
add a comment |
2 Answers
2
active
oldest
votes
up vote
1
down vote
accepted
Remove space, rename u2.email, drop the cast to text:
select ou.id as employee_org_user_id,
us.id as employee_user_id,
us.email as employee_email,
ou.manager_org_user_id as manager_org_user_id,
us2.email as manager_email
from org_users ou
JOIN users us ON ou.user_id = us.id
LEFT JOIN org_users ou1 ON ou1.id = ou.manager_org_user_id
LEFT JOIN users us2 ON ou1.user_id = us2.id
add a comment |
up vote
1
down vote
This query resolve your problem:
SELECT ou.id as employee_org_user_id,
us.id as employee_user_id,
us.email as employee_email,
ou.manager_org_user_id as manager_org_user_id
FROM org_users ou JOIN users us ON ou.user_id = us.id
Live example:
- http://tpcg.io/PuMym5
1
Thanks, it works. But I am accepting the other answer as that was marginally earlier. Thank you once again.
– Sankar
Nov 13 at 17:07
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
Remove space, rename u2.email, drop the cast to text:
select ou.id as employee_org_user_id,
us.id as employee_user_id,
us.email as employee_email,
ou.manager_org_user_id as manager_org_user_id,
us2.email as manager_email
from org_users ou
JOIN users us ON ou.user_id = us.id
LEFT JOIN org_users ou1 ON ou1.id = ou.manager_org_user_id
LEFT JOIN users us2 ON ou1.user_id = us2.id
add a comment |
up vote
1
down vote
accepted
Remove space, rename u2.email, drop the cast to text:
select ou.id as employee_org_user_id,
us.id as employee_user_id,
us.email as employee_email,
ou.manager_org_user_id as manager_org_user_id,
us2.email as manager_email
from org_users ou
JOIN users us ON ou.user_id = us.id
LEFT JOIN org_users ou1 ON ou1.id = ou.manager_org_user_id
LEFT JOIN users us2 ON ou1.user_id = us2.id
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
Remove space, rename u2.email, drop the cast to text:
select ou.id as employee_org_user_id,
us.id as employee_user_id,
us.email as employee_email,
ou.manager_org_user_id as manager_org_user_id,
us2.email as manager_email
from org_users ou
JOIN users us ON ou.user_id = us.id
LEFT JOIN org_users ou1 ON ou1.id = ou.manager_org_user_id
LEFT JOIN users us2 ON ou1.user_id = us2.id
Remove space, rename u2.email, drop the cast to text:
select ou.id as employee_org_user_id,
us.id as employee_user_id,
us.email as employee_email,
ou.manager_org_user_id as manager_org_user_id,
us2.email as manager_email
from org_users ou
JOIN users us ON ou.user_id = us.id
LEFT JOIN org_users ou1 ON ou1.id = ou.manager_org_user_id
LEFT JOIN users us2 ON ou1.user_id = us2.id
answered Nov 13 at 17:01
user8834780
552118
552118
add a comment |
add a comment |
up vote
1
down vote
This query resolve your problem:
SELECT ou.id as employee_org_user_id,
us.id as employee_user_id,
us.email as employee_email,
ou.manager_org_user_id as manager_org_user_id
FROM org_users ou JOIN users us ON ou.user_id = us.id
Live example:
- http://tpcg.io/PuMym5
1
Thanks, it works. But I am accepting the other answer as that was marginally earlier. Thank you once again.
– Sankar
Nov 13 at 17:07
add a comment |
up vote
1
down vote
This query resolve your problem:
SELECT ou.id as employee_org_user_id,
us.id as employee_user_id,
us.email as employee_email,
ou.manager_org_user_id as manager_org_user_id
FROM org_users ou JOIN users us ON ou.user_id = us.id
Live example:
- http://tpcg.io/PuMym5
1
Thanks, it works. But I am accepting the other answer as that was marginally earlier. Thank you once again.
– Sankar
Nov 13 at 17:07
add a comment |
up vote
1
down vote
up vote
1
down vote
This query resolve your problem:
SELECT ou.id as employee_org_user_id,
us.id as employee_user_id,
us.email as employee_email,
ou.manager_org_user_id as manager_org_user_id
FROM org_users ou JOIN users us ON ou.user_id = us.id
Live example:
- http://tpcg.io/PuMym5
This query resolve your problem:
SELECT ou.id as employee_org_user_id,
us.id as employee_user_id,
us.email as employee_email,
ou.manager_org_user_id as manager_org_user_id
FROM org_users ou JOIN users us ON ou.user_id = us.id
Live example:
- http://tpcg.io/PuMym5
answered Nov 13 at 17:02
ℛɑƒæĿ
98721025
98721025
1
Thanks, it works. But I am accepting the other answer as that was marginally earlier. Thank you once again.
– Sankar
Nov 13 at 17:07
add a comment |
1
Thanks, it works. But I am accepting the other answer as that was marginally earlier. Thank you once again.
– Sankar
Nov 13 at 17:07
1
1
Thanks, it works. But I am accepting the other answer as that was marginally earlier. Thank you once again.
– Sankar
Nov 13 at 17:07
Thanks, it works. But I am accepting the other answer as that was marginally earlier. Thank you once again.
– Sankar
Nov 13 at 17:07
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53285887%2fpostgresql-inner-join-and-left-join%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
JOIN users us ON ou.user_id::text = us.id::text,you need to remove,at end; alsou2.email as manager_emailshould beus2.email; no need for cast to text (though it doesn't hurt output)– user8834780
Nov 13 at 16:58