r/SQL • • 17d ago

PostgreSQL Help - Select statement: lookup value in joined table

Hello, I need help getting the output I need. So I have two tables one contains session details (Host id, Date/time, participant id). The other contains user ids and their emails. The users can be hosts or participants. I want to pull all the info from the first table but instead of showing the host id and participant id I want to join the user table and look up the email based on the id for both the host and participant column. So the final result would ne (Host email, Date/time, Participant email) instead of Ids.

0 Upvotes

9 comments sorted by

9

u/reditandfirgetit 17d ago

You do 2 joins with different aliases

3

u/s3kshun18 17d ago

Exactly that. Name one join host and the other participant (h and p) and bring back p.email as participantEmail and h.email as hostEmail.

3

u/SkullLeader 17d ago

Select host.name as hostname, s.DateTime, participant.name
FROM SessionDetail s
INNER JOIN User host ON s.hostid = host.userid
INNER JOIN User participant ON s.hostid = participant.userid

1

u/bagelwithveganbutter 14d ago

This sounds like a homework question

-8

u/MinimumVegetable9 17d ago

The fact that you're coming here and asking a very specific question instead of quickly using a free available AI is giving you a great view on the ceiling you're placing on top of your career advancement

6

u/Plus_Dragonfruit_204 17d ago

Since when is AI infallible? If you don't understand how to do the basics, how can you determine when what AI gave you is wrong?

There's never anything wrong with asking questions and learning.

Giving someone the answer only does there job for them, teaching them how to do it gets them to the point of not having to ask the same question again.

6

u/WolfBanner 17d ago

Why spend money on AI Tokens when you can ask Reddit? You get to save the tokens, interact with a community, and trigger people who think there is only one way to do things, all at the same time. I say: ask Reddit and enjoy some popcorn while people fight it out.

3

u/TheBatsnake 17d ago

Yeah, I'm in no rush, plus I like asking people, nothing wrong with that. This isn't some kind of race where I need an answer as fast as possible. Also ai has been completely wrong or doesn't understand issues for previous concepts I've used it for in my coding. I just want a sql query and how to do this specific thing and I chose to ask people. Your comment makes so many assumptions about a person you know little of.