Click here to Skip to main content
15,886,038 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Today I am "to be stuck"

I have three Tables, logical this are zwei tables Name and Room with a n to m relation.

-Name (n)

-A join table

-Room (n)

Now i want to get all Names-Rows unique, where ware "working" in room x,y and z

Please help me, today am am a big ideot ;)


User
UserID	Name	Adress	bla...
a	Max	
b	Peter
c	Ben


Join_User_Room
JoinID	UserID	RoomID
d	a	x
e	a	y
f	a	z
g	b	x
h	b	y
i	b	z


Room
RoomID	Name
x	Room1
y	Room2
z	Room3


What I have tried:

Left join and right joins and other crazy sql stuff
Posted
Updated 22-Aug-17 4:06am
Comments
OriginalGriff 22-Aug-17 6:45am    
What exactly do you want to get from that data - it's not very clear from your description.
Perhaps an example of the output you are trying to get would help?
Suvendu Shekhar Giri 22-Aug-17 6:54am    
I agree with @OriginalGriff. JOIN is not just for joining to 2 tables rather it's for joining 2 tables to get some meaningful data.
BoySetsFire 22-Aug-17 6:56am    
select * from User

join Join_User_Room on UserID	= UserID	
join Room on RoomID = RoomID 

where  RoomID in( X, Y, Z)


now has the result set to many rows. from the join table... but, i want to see onley two rows, the row from user a and the row from user b...


Br,

Benny
OriginalGriff 22-Aug-17 9:06am    
So show us an example of the output you are trying to get, and the output you get now.
Bryian Tan 22-Aug-17 10:52am    
To continue with the example you provided, you can modify the query to something like below

select DISTINCT u.UserId, u.Name from [User] u
join Join_User_Room jur on u.UserID	= jur.UserID	
join Room r on jur.RoomID = r.RoomID 
where  jur.RoomID in( 'X', 'Y', 'Z')

1 solution

Try this:
SQL
SELECT u.* FROM [User] u
JOIN (SELECT ju.UserID FROM Join_User_Room ju GROUP BY ju.UserID) jg ON jg.UserID = u.UserID

And when you post code fragments (such as your SQL) please try to ensure they work: that won't for loads of reasons!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900