簡體   English   中英

從同一表中獲取單列記錄

[英]Get the single column records from the same table

從數據表中檢索記錄時,我遇到了一些問題。 有人可以給我任何建議嗎?

Id LearnerId ConnectionId  IsApproved   
3   1            38       1
5   39           1        1 
7   1            31       1 
13  1            30       1 
31  1            40       1 
34  41           1        1 
35  31           1        1 
39  1            42       1 

這就是我的桌子的樣子。 我想在此表上使用選擇查詢來選擇記錄,如下所示:

AllId
38
39
31
30
40
41
31
42

我如何編寫查詢才能找到上述記錄列表? 我想收集除1以外的所有ID。

您可以在兩個查詢之間使用UNION ALL 第一個將返回connectionid值,第二個將返回learnerid值:

select ConnectionId as AllId
from LearnerConnections
where LearnerId = 1
union all
select LearnerId as AllId
from LearnerConnections
where ConnectionId = 1

參見帶有演示的SQL Fiddle

SELECT CASE LearnerId
            WHEN 1 THEN ConnectionId
            ELSE LearnerId
       END AS AllId
FROM tablename
select  Id
from    YourTable
union all
select  LearnerId
from    YourTable
union all
select  ConnectionId
from    YourTable
SELECT  Id AS AllId
FROM    YourTable
union all
SELECT  LearnerId AS AllId
FROM        YourTable
union all
SELECT  ConnectionId AS AllId
FROM        YourTable
SELECT LearnerId AS AllId FROM tableName
WHERE ConnectionId = 1
UNION
SELECT ConnectionId AS AllId FROM tableName
WHERE LearnerId = 1

暫無
暫無

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

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