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
...
Related
So I have a query that I run that basically looks like the one below (simplified it though for easier viewing). When I search on user_orders.orderID it works flawlessly, but if I try to search by user_orders.reference instead it just timeouts.
Now I assume that this has something to do with some of the tables that I have joined not having the reference number in them, only the orderID is present in all of them. However it would greatly simplify my work if I could search on the reference number directly, instead of the orderID.
Any advice on how to solve it?
EDIT:
Solved now, thanks everyone! It was indeed the FULL OUTER JOIN that was causing the problems, didn't fully understand what it did compared to a regular JOIN.
SELECT
user_orders.reference,
user_orders.orderid,
transfers.username,
notifications.datestamp,
orders.refund,
users.acac,
refunds.state,
decisionlog.data
FROM user_orders
FULL OUTER JOIN decisionlog ON user_orders.orderid = decisionlog.orderid
FULL OUTER JOIN refunds ON user_orders.orderid = refunds.orderid
FULL OUTER JOIN notifications ON user_orders.orderid = notifications.orderid
JOIN transfers ON user_orders.orderid = transfers.orderid
JOIN orders ON transfers.orderid = orders.orderid
JOIN users ON transfers.username = users.username
WHERE user_orders.orderid = xxx;
As suggested in the comments, if you do not have an orderid in any of decisionlog, refunds, notifications tables, this query will return a null for notifications.datestamps, refunds.state, decisionlog.data , but it will not give you an error.
Plus, take into account that using JOIN (INNER JOIN) will return you only orderid's that are on the transfer and order table and whose users are found at users table
SELECT
user_orders.reference,
user_orders.orderid,
transfers.username,
notifications.datestamp,
orders.refund,
users.acac,
refunds.state,
decisionlog.data
FROM user_orders
LEFT JOIN decisionlog ON user_orders.orderid = decisionlog.orderid
LEFT JOIN refunds ON user_orders.orderid = refunds.orderid
LFET JOIN notifications ON user_orders.orderid = notifications.orderid
INNER JOIN transfers ON user_orders.orderid = transfers.orderid
INNER JOIN orders ON transfers.orderid = orders.orderid
INNER JOIN users ON transfers.username = users.username
WHERE user_orders.reference = xxx;
I am trying to JOIN four tables together, one table is a joining table as two of the tables have many-to-many relationship:
What would be the query to select DispalyName from User, Name & Description from Role and Permission & Description from Permission.
I know how to join two tables together, but my method of doing so does not work on this problem.
I have tried the following query but it doesn't seem to like it.
SELECT org.[User].[DisplayName], org.[Role].[Description], org.[Permission].[Description]
FROM org.[Role] rolee
JOIN org.[RolePermissions] rolePerms ON rolee.ID = rolePerms.RoleId
JOIN org.[Permission] perms ON rolePerms.PermissionId = perms.ID
WHERE [User].[email] LIKE '%myemail%'
Try this - It seems that You forget to join with the user table
SELECT org.[User].[DisplayName], org.[Role].[Description], org.[Permission].[Description]
FROM org.[User] user Join org.[Role] rolee on user.RoleID = rolee.ID
JOIN org.[RolePermissions] rolePerms ON rolee.ID = rolePerms.RoleId
JOIN org.[Permission] perms ON rolePerms.PermissionId = perms.ID
WHERE org.[User].[email] LIKE '%myemail%'
You need to join User
SELECT u.[DisplayName], r.[Description], p.[Description]
FROM org.[Role] r
JOIN org.[RolePermissions] rp ON r.ID = rp.RoleId
JOIN org.[Permission] p ON rp.PermissionId = p.ID
JOIN org.[User] u ON r.Id = u.RoleID
WHERE u.email LIKE '%myemail%'
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
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
I'm trying to join multiple tables together for building a report. The report lists a course, revisions made to it, and who requested, made and approved the revisions.
Under requested, made an approved, the values are employee numbers. I'm trying to join my innerjoined table above, with the Employee table so I can list the names (not just employee numbers) of those that requested, made and approved revisions.
This is what I have which I know is totally wrong.
SELECT *
FROM Courses
INNER JOIN CourseRevisions ON CourseRevisions.PELID = Courses.PELID
INNER JOIN CourseGroups ON CourseGroups.CourseGroupID = Courses.CourseGroupID
INNER JOIN [dbo].[OPG_Employees] ON OPG_Employees.EmployeeID = CourseRevisions.UpdatedBy
AND OPG_Employees.EmployeeID = CourseRevisions.ApprovedBy
AND OPG_Employees.EmployeeID = CourseRevisions.RequestedBy
This only returns a single result which just happens to have the same employee ID listed for all 3 (Requested, Approved and Updated)
How would i get it so I can get the table result for individual employees in each?
You have to join to the OPG_Employees table once for each field, i.e. 3 times in the example above. One INNER JOIN to it for UpdatedBy, one INNER JOIN for ApprovedBy, one INNER JOIN for RequestedBy.
Something like so:
SELECT *
FROM Courses
INNER JOIN CourseRevisions ON CourseRevisions.PELID = Courses.PELID
INNER JOIN CourseGroups ON CourseGroups.CourseGroupID = Courses.CourseGroupID
INNER JOIN [dbo].[OPG_Employees] empUpdatedBy ON empUpdatedBy.EmployeeID = CourseRevisions.UpdatedBy
INNER JOIN [dbo].[OPG_Employees] empApprovedBy ON empApprovedBy.EmployeeID = CourseRevisions.ApprovedBy
INNER JOIN [dbo].[OPG_Employees] empRequestedBy ON empRequestedBy.EmployeeID = CourseRevisions.RequestedBy
You need a separate join for each employee being referenced:
SELECT *
FROM Courses INNER JOIN
CourseRevisions
ON CourseRevisions.PELID = Courses.PELID INNER JOIN
CourseGroups
ON CourseGroups.CourseGroupID = Courses.CourseGroupID INNER JOIN
[dbo].[OPG_Employees] UpdateEmp
ON UpdateEmp.EmployeeID = CourseRevisions.UpdatedBy INNER JOIN
[dbo].[OPG_Employees] ApprovedEmp
on OPG_ApprovedEmp.EmployeeID = CourseRevisions.ApprovedBy INNER JOIN
[dbo].[OPG_Employees] RequestedEmp
on RequestedEmp.EmployeeID = CourseRevisions.RequestedBy
Your original formulation required that all three ids be exactly the same.