簡體   English   中英

在我的情況下,如何使用SQL Server將列記錄放入行中?

[英]How to get column records into row in my case using SQL Server?

我有一個名為RelationData的表,該表有4列:

RelationData

Parent    child1    child2    child3
------------------------------------
111       112       113       117
111       222       223       224
444       441       442       443 

我想在一行中顯示任何ID的每一個是否匹配。

如果用戶搜索111或112或113或117或222或223或224,則必須顯示

111
112
113 
117
222
223
224   

如果用戶搜索442,則結果應為

444
441
442
443 

即使使用case ,它也會顯示在列中,而不是按行顯示

您可以取消顯示結果。 這是一種方法:

select v.child
from relationdata rd outer apply
     (values (rd.child1), (rd.child2), (rd.child3), (rd.child4)) v(child)
where 111 in (rd.child1, rd.child2, rd.child3, rd.child4);

注意:有四列帶有類似引用的引用通常表明數據庫設計不佳。 最好有一個帶有單個child列和一個“子編號”的關系表。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM