SQL: Join Parent - Child tables - sql

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

Related

Difficulty nailing the syntax for a PostgreSQL query to grab timeline of posts

I'm new to PG and having trouble getting the syntax of this query right. I keep getting ERROR: missing FROM-clause entry for table "post_reactions". I'm sure it's a dumb oversight.
I'm trying to build your typical posts list with:
all columns and all rows from the posts table where users.id = 'whatever'
all the post reactions from the post_reactions table where the posts.id = post_reactions.post_id
the reaction keywords ('happy', 'sad', etc.) from the reactions table where the reaction.id = post_reactions.reaction_id
the display_name from the users table associated with each post reaction by matching the post_reaction.user_id with users.id and returning the users.disaplay_name from the users table.
SELECT
post_reactions.post_id,
post_reactions.reaction_id,
post_reactions.user_id,
posts.content,
posts.content,
posts.create_timestamp,
posts.id,
posts.image_url,
posts.updated_at,
reactions.id,
reactions.keyword,
users.display_name,
users.id
FROM public.posts
LEFT OUTER JOIN users ON (users.id = post_reactions.user_id)
LEFT OUTER JOIN post_reactions ON (posts.id = post_reactions.post_id)
LEFT OUTER JOIN reactions ON (reactions.id = post_reactions.reaction_id)
WHERE users.id = 'foobar'
;
The tables need to be included in the FROM clause before they can be referenced in the ON:
FROM public.posts LEFT JOIN
post_reactions
ON posts.id = post_reactions.post_id LEFT JOIN
reactions
ON reactions.id = post_reactions.reaction_id LEFT JOIN
users
ON users.id = post_reactions.user_id
I would also advise you to use table aliases so the query is easier to write and to read:
FROM public.posts p LEFT JOIN
post_reactions pr
ON p.id = pr.post_id LEFT JOIN
reactions r
ON r.id = pr.reaction_id LEFT JOIN
users u
ON u.id = pr.user_id
The aliases, though, have to be used throughout the query.

How to join tables having many to many relationship

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%'

Inner Join PostgreSQL

I'm trying to join two table together with this query
select comments.* ,username, title
from comments INNER JOIN users ON comments.user_id = users.user_id
INNER JOIN posts ON comments.post_id = posts.post_id;
the result given back is column reference "username" is ambiguous
Columns should be in form table.column
select comments.* ,username, title
should be
select comments.* ,YOUR_TABLE.username, YOUR_TABLE.title
SELECT comments.* ,users.username, title
FROM comments INNER JOIN users ON comments.user_id = users.user_id
INNER JOIN posts ON comments.post_id = posts.post_id;
Note the users.username in the query above.
As an aside, you shouldn't have a column username in multiple tables. You might want to look into "normalizing" your data.

Sql match id's with names from a table that matches with another table

I have a table friend which has two columns with Id's that belong to specific usernames. See example:
Id_Gebruiker is the user and Id_Gebruiker2 is the friend of the user.
The table friend has id's that belong to the table user.
Then that table user has Foreign Keys (column Id_Client) to the table client (referencing the Id_Client column). The client table contains the usernames.
Now I want to get the corresponding usernames that belongs to the Id's that the Friend table contains. So far I got it working when I use only two tables but I can't get it to work with three tables.
Something like this?
select f.Id_Gebruiker, f.Id_Gebruiker2, u.Id_Client, c.Gebruikersnaam
from friend f
join [user] u on f.Id_Gebruiker2 = u.Id
join client c on u.id_client = c.id
EDIT Assuming the names of the users are on the same table (client) and that every friend.Id_Gebruiker corresponds to a client.Id (so there exists a FK relationship), you can find both names with an extra join:
select c.Gebruikersnaam as UserName, c2.Gebruikersnaam as FriendName
from friend f
join client c on f.Id_Gebruiker = c.Id
join [user] u on f.Id_Gebruiker2 = u.Id
join client c2 on u.id_client = c2.id
Here is another answer that you can check and verify at http://sqlfiddle.com/#!3/2b9dd/1.
The main query for getting names for userid and friendid in friends table is as below.
SELECT f1.userid,
f1.friendid,
y.username AS UserName,
x.username AS FriendName
FROM friends f1
INNER JOIN (SELECT DISTINCT f.friendid,
c.clientname AS UserName
FROM users u
INNER JOIN clients c
ON u.id_client = c.id
INNER JOIN friends f
ON f.friendid = u.id) x
ON f1.friendid = x.friendid
INNER JOIN (SELECT DISTINCT f.userid,
c.clientname AS UserName
FROM users u
INNER JOIN clients c
ON u.id_client = c.id
INNER JOIN friends f
ON f.userid = u.id) y
ON f1.userid = y.userid;

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
...