簡體   English   中英

獲取同一表中的相關行?

[英]Get related rows in the same table?

我正在為此苦苦掙扎。 我需要從同一張表中選擇一行和其他相關的行。 這是表格的示例:

table
key    |    value    |    related
=================================
1      |    omg      |    0
2      |    lol      |    0
3      |    rofl     |    2
4      |    barfoo   |    0
5      |    foo      |    0
6      |    bar      |    0
...
20000  |    haha     |    2

(其中相關的“ 2”是“ lol”的行鍵)

因此,在我同時(不同時)這樣做的情況下:

SELECT * FROM table Where value='lol'

或這個

SELECT * FROM table Where value='rofl'

或這個

SELECT * FROM table Where value='haha'

它應該返回:

key    |    value    |    related
=================================
2      |    lol      |    0
3      |    rofl     |    2
20000  |    haha     |    2

有想法嗎?

非常感謝!

嘗試這個:

Select * From table t 
Where Exists 
   (Select * From Table
    where value = @theValue
       and valueKey in (t.ValueKey, t.related))         

暫無
暫無

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

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