Two inner joins with same tables - sql

I have this problem I've been working on for a while, and I just haven't been able to figure out what's wrong with my query. Today, I finally got a bit closer, and now I think I'm pretty close and not too far from succeeding, but I need your help to spot the mistake in this query, because I think I've stared blind on it. This is the error I get:
"The objects "webpages_UsersInRoles" and "webpages_UsersInRoles" in the FROM clause have the same exposed names. Use correlation names to distinguish them."
SELECT * FROM Users
INNER JOIN webpages_UsersInRoles
ON webpages_UsersInRoles.UserId = Users.UserId
INNER JOIN webpages_UsersInRoles
ON webpages_UsersInRoles.RoleId = webpages_Roles.RoleId
WHERE Users.UserId = 1
Thank you in advance!

You have to use an alisas for the tables:
SELECT * FROM Users
INNER JOIN webpages_UsersInRoles w1 ON w1.webpages_UsersInRoles.UserId = Users.UserId
INNER JOIN webpages_UsersInRoles w2 ON w1.webpages_UsersInRoles.RoleId = w2.webpages_Roles.RoleId
WHERE Users.UserId = 1

I think below is the final query that you need. I have used alias as TAB1,TAB2 and TAB3 for clarity and to avoid confusion. Change it as required.
SELECT * FROM Users TAB1
INNER JOIN webpages_UsersInRoles TAB2
ON TAB2.UserId = TAB1.UserId
INNER JOIN webpages_Roles TAB3
ON TAB2.RoleId = TAB3.RoleId
WHERE TAB1.UserId = 1
Btw,JUST FOR INFO.... probably you get "The objects "webpages_UsersInRoles" and "webpages_UsersInRoles" in the FROM clause have the same exposed names. Use correlation names to distinguish them." error in the line mentioned below from your original query:
SELECT * FROM Users
INNER JOIN webpages_UsersInRoles
ON webpages_UsersInRoles.UserId = Users.UserId
INNER JOIN webpages_UsersInRoles
ON webpages_UsersInRoles.RoleId -----> Not able to identify which webpages_UsersInRoles is being called (First inner join or second inner join)
= webpages_Roles.RoleId
WHERE Users.UserId = 1

I see 3 tables ... Users, webpages_UsersInRoles and webpages_Roles
Only 2 are called.
webpages_Roles ??
Maybe with webpages_Roles in the second join :
SELECT * FROM Users
INNER JOIN webpages_UsersInRoles
ON webpages_UsersInRoles.UserId = Users.UserId
INNER JOIN webpages_Roles
ON webpages_UsersInRoles.RoleId = webpages_Roles.RoleId
WHERE Users.UserId = 1

Related

SQL 2 Inner Joins on the same field

I have 3 tables as seen from the image below. I have been able to join the projects.project_id to the sprints.project_id and the projects.manager_id to the users.user_id however, also want to join the users.user_id to the projects.product_owner_id I had tried to
INNER JOIN users ON projects.manager_id = users.user_id
AND projects.product_owner_id = users.user_id
Although this returned now results. I want in my list view to be able to list the Manager ID and Products Owner ID in a ListView Control.
Below is my current SQL Query any help would be appreciated.
SELECT
projects.project_name, projects.manager_id,
projects.product_owner_id, projects.project_description,
projects.status,
sprints.sprint_name, sprints.sprint_start_date,
sprints.sprint_length, sprints.work_mon,
sprints.work_tue, sprints.work_wed, sprints.work_thu,
sprints.work_fri, sprints.work_sat, sprints.work_sun,
projects.project_id, sprints.project_id AS Expr1,
sprints.sprint_id, users.username
FROM
projects
INNER JOIN
users ON projects.manager_id = users.user_id
INNER JOIN
sprints ON projects.project_id = sprints.project_id
WHERE
(projects.project_id = #project_id)
Replace inner join with left outer join (since there are products without owner filled) and make a separate join the two associations - manager and owner:
...
INNER JOIN users manager ON projects.manager_id = manager.user_id
LEFT OUTER JOIN users owner ON projects.product_owner_id = owner.user_id
...

ORA-00904 when using OUTER JOIN with 3 tables in clause

SELECT
s.session_name semester
FROM
lsx.elsp_session s,
lsx.elsp_course_offering co,
its.ACCOUNT ac
LEFT OUTER JOIN lsx.elsp_class_attendance ca on ca.course_id = CO.ID and ca.student_number = ac.id
WHERE co.session_id = s.session_id
I am getting an ORA-00904 error and the problem seems to be with "CO.ID" being an invalid identifier. However, if I put the "lsx.elsp_course_offering co" table to be last in the FROM list, then "ac.id" becomes the problem identifier.
Is it not possible to use another table in a JOIN clause? I seem to recall creating successful queries like this using MySQL.
I am using Oracle 9.2.0.8.0.
Following the advice I received, I was able to get the join working as expected when I reworked the query to this:
SELECT s.session_name semester FROM lsx.elsp_session s
INNER JOIN lsx.elsp_course_offering co
ON co.session_id = s.session_id
LEFT OUTER JOIN lsx.elsp_class_attendance ca
ON ca.course_id = co.id
LEFT OUTER JOIN its.account ac
ON ca.student_number = ac.id
Many thanks for your help.
You may have misunderstood the comment above. Generally people do not mix the multiple FROM tables with JOIN syntax.
I think you should write it this way instead. It should be easier to follow this way.
SELECT s.session_name semester
FROM lsx.elsp_session s
INNER JOIN lsx.elsp_course_offering co
ON co.session_id = s.session_id
INNER JOIN its.account ac
ON ca.student_number = ac.id
LEFT OUTER JOIN lsx.elsp_class_attendance ca
ON ca.course_id = co.id
and then see if you get syntax errors.

t-sql query that returns missing records

I have a query (ContactFormTypesRequired) that returns ContactID and FormTypeID utilizing related tables that are not shown below. This is a list of FormTypes that each Contact should have related to it as a Form.
I need a query that returns Contacts that do not have one or more related forms of the FormTypes specified in the above query.
I've tried a left outer join from Form to ContactsFormTypesRequired on FormTypeID, but the results don't take into account FormTypes that each specific Contact should have.
Please let me know if you have any questions.
Thank you in advance for any suggestions.
I am writing the query this way. This first starts with your query to get needed forms as a CTE, then cross joins them to Contacts to get every needed combination, before left joining to the actual forms.
with NeededForms (<yourqueryhere>)
select distinct c.*
from Contact c cross join
NeededForms nf left outer join
Form F
on nf.FormTypeId = f.FormTypeId left outer join
ContactForm cf
on c.ContactId = cf.ContactId and
f.FormId = cf.FormId
where cf.FormId is null
I'm doing it this way, so you can answer the query of what forms are missing with a very similar query:
with NeededForms (<yourqueryhere>)
select c.*, nf.FormTypeId
from Contact c cross join
NeededForms nf left outer join
Form F
on nf.FormTypeId = f.FormTypeId left outer join
ContactForm cf
on c.ContactId = cf.ContactId and
f.FormId = cf.FormId
where cf.FormId is null
Try this simple query using NOT IN Cluase:
SELECT * FROM Contact
WHERE ContactID IN
(SELECT ContactID FROM ContactForm
INNER JOIN FORM ON ContactForm.FormID=Form.FormID
WHERE FormTypeID=#FormTypeID)
I created ContactFormTypeExist:
SELECT DISTINCT Contact.ContactID, Form.FormTypeID
FROM Contact
INNER JOIN ContactForm
ON Contact.ContactID = ContactForm.ContactID
INNER JOIN Form
ON ContactForm.FormID = Form.FormID
Then joined ContactFormTypesRequired described in the question above to ContactFormTypeExist with outer join to give me the ConactIDs that are missing related FormTypeIDs:
SELECT ContactFormTypesRequired.ConactID
FROM ContactFormTypeExist
RIGHT JOIN ContactFormTypesRequired
ON (ContactFormTypeExist.FormTypeID = ContactFormTypesRequired.FormTypeID)
AND (ContactFormTypeExist.ConactID = ContactFormTypesRequired.ConactID)
WHERE (((ContactFormTypeExist.ConactID) Is Null));
This returns all the ContactID for Contacts missing FormTypes required by their ContactType.

SQL: Join Parent - Child tables

I'm building a simple review website application and need some help with SQL Query.
There are 3 tables (Topics, Comments, Users). I need a SQL query to select the data from all 3 tables.
The 'Topics' table is the parent and the 'Comments' table contains the child records (anywhere from zero to 100 records per parent.
The third table 'Users' contains the user information for all users.
Here are the fields for the 3 tables:
Topics (topicID, strTopic, userID)
Comments (commentID, topicID, strComment, userID)
Users (userID, userName)
I tried:
SELECT *
FROM Topics
Inner Join Comments ON Topics.topicID = Comments.topicID
Inner Join Users ON Topics.userID = Users.userID
But this does not work correctly because there are multiple topics and the User info is not joined to the Comments table. Any help would be appreciated.
You should do left join with Comment to get Topics with no comments and also join Topic and Comment with Users to get related user information for both.
SELECT *
FROM Topics t
INNER JOIN Users tu on tu.userID = t.userID
LEFT JOIN Comments c on c.topicID = t.topicID
LEFT JOIN User cu on cu.userID = c.userID
You need to join to the user table twice.
SELECT *
FROM Topics
INNER JOIN Comments ON Topics.topicID = Comments.topicID
INNER JOIN Users AS u1 ON Topics.userID = u1.userID
INNER JOIN Users AS u2 ON Comments.userID = u2.userID

Two left joins and a union in MySQL

I'm trying to do a pretty complex query in MySQL; complex for me, at least.
Here's an example of what I'm trying to do:
SELECT * FROM friends
LEFT JOIN users ON users.uid = friends.fid1
LEFT JOIN users ON users.uid = friends.fid2
WHERE (friends.fid1 = 1) AND (friends.fid2 > 1)
UNION SELECT fid2 FROM friends
WHERE (friends.fid2 = 1) AND (friends.fid1 < 1)
ORDER BY RAND()
LIMIT 6;
I'm getting back: ERROR 1066 (42000): Not unique table/alias: 'users'.
Where am I going wrong, and how should I really be performing this query?
Alias your table, like:
LEFT JOIN users u1 ON u1.uid = friends.fid1
LEFT JOIN users u2 ON u2.uid = friends.fid2
You have written left join two times with different fields of tables, it seems to When it got parse so give them alias and then join them with friends Table
LEFT JOIN users users1 ON users1.uid = friends.fid1
LEFT JOIN users users2 ON users2.uid = friends.fid2