Inner Join PostgreSQL - sql

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.

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.

Access SQL LEFT JOIN not returning results

I have three tables: Comments, Users and CommentsHelpfulness.
Users can submit several comments, which are stored in the Comments table.
CommentsHelpfulness has three columns: UserID, CommentID and a "Helpful" Boolean. Every user can indicate for every comment if they find it useful, which will create an entry in the CommentsHelpfulness table.
I want a query that gives me all Comment IDs, with the name of the user that submitted it and shows whether the currently logged in user found it helpful, did not find it helpful or did not say anything about it. So the ID of a comment the current user did not express his opinion about should still be output, just without the helpful Boolean.
To me that sounds like it should be done like this using a left join:
SELECT Comments.ID, Users.Nom, CommentsHelpfulness.Helpful
FROM (Comments INNER JOIN Users
ON Comments.UserID = Users.ID)
LEFT JOIN CommentsHelpfulness
ON (CommentsHelpfulness.CommentID = Comments.ID
AND (CommentsHelpfulness.UserID = ?))
Unfortunately this does not output Comment IDs without an entry in the CommentsHelpfulness table. Why does this not work? Is it because of Access?
I think the issue is the inner join, not the left join.
Try removing that table:
SELECT c.ID, ch.Helpful
FROM Comments as c LEFT JOIN
CommentsHelpfulness as ch
ON ch.CommentID = c.ID AND
ch.UserID = ?
select c.id, c.Nom, d.Helpful from (
select a.ID, b.Nom from Comments a left join Users b
on a.UserID = b.ID) c left join CommentsHelpfulness d
on d.CommentID = c.ID;

sql - How to have multiple select/from statements in one query

I'm trying to pull a report where each column is selecting from a specific table set. However, one of the columns needs to pull from a completely different table set and still be included in the same report. Of course, this doesn't work:
select u.first_name, ticket_work.time_spent
FROM tickets LEFT OUTER JOIN ticket_work ON ticket_work.ticket_id = tickets.id JOIN users u
(select count(tickets.id) FROM tickets JOIN users u)
where tickets.assigned_to = u.id
...
So just the part (select count(tickets.id) FROM tickets JOIN users u) needs to be selecting from the different table set but still be included in the report.
I'm a little confused by your question. Are you wanting to return the user, the count of tickets for that user, and the amount of time spent overall? If so, something like this should work:
select u.id, u.first_name,
SUM(tw.time_spent) summed_time_spent,
COUNT(DISTINCT t.id) count_tickets
FROM users u
LEFT JOIN tickets t
ON u.id = t.assigned_to
LEFT JOIN ticket_work tw
ON tw.ticket_id = t.id
GROUP BY u.id, u.first_name
Your questions is unclear, but just generally, it sounds like you're trying to join to a derived table (i.e., a query). In that case, do this:
SELECT...
FROM...
table_A A LEFT JOIN
(SELECT keyfield, valuefield FROM table_b WHERE ...) B
ON A.keyfield = B.keyfield
Does that make sense? To make a derived table, you put a query inside of parenthesis, give it an alias ('B' in this case), and then join it to your other tables as though it were a regular table.
Don't know about your table structure but you may use a sub query for such requirement
select u.first_name, ticket_work.time_spent,(select count(tickets.id) FROM tickets where ticket.id=ticket_work.ticket_id) as myCount
FROM tickets LEFT OUTER JOIN ticket_work ON ticket_work.ticket_id = tickets.id JOIN users u
where tickets.assigned_to = u.id

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

MySQL JOIN / GROUP_CONCAT second table?

So I have this query that works perfectly:
SELECT users.*,
GROUP_CONCAT(categories.category_name) AS categories
FROM users
LEFT OUTER JOIN user_categories ON users.user_id = user_categories.user_id
LEFT OUTER JOIN categories ON user_categories.category_id = categories.category_id
WHERE users.user_city = 'brooklyn'
GROUP BY users.user_id
LIMIT 10;
Say I have another table that holds phone numbers, for the "users" a user can have any number of phone numbers... How would I go about doing round about the exact same thing I am doing wit the categories? In other words, I would like to get another column with ALL of the phone_numbers found in the "phones" table that have the same "user_id" and concat them together(phone1, phone2, phone3)? I have tried:
SELECT users.*,
GROUP_CONCAT(phones.phone_number) AS phone_numbers,
GROUP_CONCAT(categories.category_name) AS categories
FROM users
LEFT OUTER JOIN phones ON users.user_id = phones.user_id
LEFT OUTER JOIN user_categories ON users.user_id = user_categories.user_id
LEFT OUTER JOIN categories ON user_categories.category_id = categories.category_id
WHERE users.user_city = 'brooklyn'
GROUP BY users.user_id
LIMIT 10;
With no luck... or at least the query executes but it does some weird duplication thing... any help would be awesome!
Thanks!
It does weird things, becaue there is a cross product of certain rows. You can use the DISTINCT keyword to get only unique phone numbers:
GROUP_CONCAT(DISTINCT phones.phone_number) AS phone_numbers,
Check the documentation. Alternatively, you can get the phone numbers in another query where you would select only the phone numbers with a condition like WHERE phones.user_id IN (x, x, x, ...) (x are IDs returned from the first query).
This happened to me, I later had to alter my query to this.
SELECT
( SELECT GROUP_CONCAT(`partnumber`) FROM `product_partnumber` AS `n` WHERE `p`.`id`=`n`.`product_id`) as `partnumbers`,
( SELECT GROUP_CONCAT(`oem`) FROM `product_oem` AS `n` WHERE `p`.`id`=`n`.`product_id`) as `oems`
FROM `product` AS `p`
So I had to use sub queries else I had the duplication.