将多个连接表中的重叠行折叠成尽可能少的行

发布于 2024-08-24 10:10:13 字数 1019 浏览 3 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

清秋悲枫 2024-08-31 10:10:13

我假设您想要将带有尺寸的第 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.

萌无敌 2024-08-31 10:10:13

我真的不喜欢以这种方式组合数据,但你可以这样做

Select Design_Id, 'Color' as Type, Color as Value from Table_Color where Design_Id = 'Alpha'
union all
Select Design_Id, 'Size' as Type, Size as Value from Table_Size where Design_Id = 'Alpha'

Design_Id Type  Value
========= ====  =====
Alpha     Color Black
Alpha     Color Blue
Alpha     Color Red
Alpha     Color Green
Alpha     Size  S
Alpha     Size  M
Alpha     Size  L

I don't really favor combining data in this way, but you could do something like the following:

Select Design_Id, 'Color' as Type, Color as Value from Table_Color where Design_Id = 'Alpha'
union all
Select Design_Id, 'Size' as Type, Size as Value from Table_Size where Design_Id = 'Alpha'

which gives

Design_Id Type  Value
========= ====  =====
Alpha     Color Black
Alpha     Color Blue
Alpha     Color Red
Alpha     Color Green
Alpha     Size  S
Alpha     Size  M
Alpha     Size  L
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文