TSQL关于连接表和n-m关系的东西 [英] TSQL something about join table and n - m relations

查看:97
本文介绍了TSQL关于连接表和n-m关系的东西的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

今天我被卡住了



我有三张桌子,逻辑上这是zwei桌子名称和房间的关系。



-Name(n)



-A加入表



-Room(n)



现在我想让所有名字 - 行独特,其中洁具工作在房间x,y和z



请帮助我,今天我是一个伟大的理想选择;)





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

推荐答案

试试这个:

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



当您发布代码片段(例如您的SQL)时,请尽量确保它们正常工作:这不会出于多种原因!


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


这篇关于TSQL关于连接表和n-m关系的东西的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆