Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Help needed with query please

Author  Topic 

bpsintl
Posting Yak Master

132 Posts

Posted - 2009-08-21 : 04:57:17
Hi, I have 3 tables, users, venues and services.

A user can either have a venue associated to him, a service associated to him or both.

Basically I need a query that will pull the user, and either the venues record or

service record or both that are associated with him.

The users table pk is user_id. The venue and services tables have a fk of userid.

User table
User_id
username

Venues table
venue_id
venue_name
userid

services table
service_id
service_name
userid

(other fields left out that are not needed for this query)

Hope that makes sense!

YellowBug
Aged Yak Warrior

616 Posts

Posted - 2009-08-21 : 06:53:11
Think you need the LEFT JOIN, like this:
SELECT *
FROM UserTable U
LEFT JOIN VenuesTable V ON U.User_id = V.userid
LEFT JOIN ServicsTable S ON U.User_id = S.userid

It's usually a good idea to keep the field names the same in different tables, i.e. use User_id always, instead of User_id and userid.
Go to Top of Page

bpsintl
Posting Yak Master

132 Posts

Posted - 2009-08-21 : 08:23:28
Great, thanks for that. I didn't think of left joins!
Go to Top of Page
   

- Advertisement -