将多个连接表中的重叠行折叠成尽可能少的行
Table_Design:
ID
"Alpha"
"Beta"
Table_Size:
Design_ID Size
"Alpha" S
"Alpha" M
"Alpha" L
"Beta" S
"Beta" L
Table_Color:
Design_Id Color
"Alpha" "Black"
"Alpha" "Blue"
"Alpha" "Red"
"Alpha" "Green"
"Beta" "Orange"
select D.ID, S.Size, C.Color from
Table_Design D
Left Outer Join
Table_Size S
ON D.ID = S.Design_Id
Left Outer Join
Table_Color C
ON D.ID = C.Design_Id
where
D.ID = 'Alpha'
Yields:
D.ID S.Size C.Color
Alpha S Black
Alpha S Blue
Alpha S Red
Alpha S Green
Alpha M Black
Alpha M Blue
Alpha M Red
Alpha M Green
Alpha L Black
Alpha L Blue
Alpha L Red
Alpha L Green
是否有办法编写一个查询来返回此值:
D.ID S.Size C.Color
Alpha S Black
Alpha M Blue
Alpha L Red
Alpha Green
Table_Design:
ID
"Alpha"
"Beta"
Table_Size:
Design_ID Size
"Alpha" S
"Alpha" M
"Alpha" L
"Beta" S
"Beta" L
Table_Color:
Design_Id Color
"Alpha" "Black"
"Alpha" "Blue"
"Alpha" "Red"
"Alpha" "Green"
"Beta" "Orange"
select D.ID, S.Size, C.Color from
Table_Design D
Left Outer Join
Table_Size S
ON D.ID = S.Design_Id
Left Outer Join
Table_Color C
ON D.ID = C.Design_Id
where
D.ID = 'Alpha'
Yields:
D.ID S.Size C.Color
Alpha S Black
Alpha S Blue
Alpha S Red
Alpha S Green
Alpha M Black
Alpha M Blue
Alpha M Red
Alpha M Green
Alpha L Black
Alpha L Blue
Alpha L Red
Alpha L Green
Is there anyway to write a query to return this instead:
D.ID S.Size C.Color
Alpha S Black
Alpha M Blue
Alpha L Red
Alpha Green
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我假设您想要将带有尺寸的第 n 行与带有颜色的第 n 行组合起来。对于您所展示的表格设计,这是不可能的,因为您无法可靠地识别表格的第 n 行。
我想说的是重新设计:创建一个单独的表来链接具有尺寸和颜色(可以为 NULL)的设计,并且您的查询会容易得多。
I assume that you want to combine the n-th line with sizes with the n-th line with colors. With the table design you are showing it will not be possible because you can't reliably identify the n-th line the tables.
I'd say redesign: make a separate table to link a design with both a size and a color (which can be NULL) and your queries will be a lot easier.
我真的不喜欢以这种方式组合数据,但你可以这样做
:
I don't really favor combining data in this way, but you could do something like the following:
which gives