0

我在从我的数据表中检索记录时遇到了一些问题。有人可以给我任何建议吗?

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。

4

5 回答 5

3

您可以UNION ALL在两个查询之间使用 a。第一个将返回connectionid值,第二个将返回learnerid值:

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

请参阅带有演示的 SQL Fiddle

于 2013-02-05T11:10:24.577 回答
2
SELECT CASE LearnerId
            WHEN 1 THEN ConnectionId
            ELSE LearnerId
       END AS AllId
FROM tablename
于 2013-02-05T11:10:33.670 回答
1
select  Id
from    YourTable
union all
select  LearnerId
from    YourTable
union all
select  ConnectionId
from    YourTable
于 2013-02-05T11:09:41.097 回答
0
SELECT  Id AS AllId
FROM    YourTable
union all
SELECT  LearnerId AS AllId
FROM        YourTable
union all
SELECT  ConnectionId AS AllId
FROM        YourTable
于 2013-02-05T11:10:52.840 回答
0
SELECT LearnerId AS AllId FROM tableName
WHERE ConnectionId = 1
UNION
SELECT ConnectionId AS AllId FROM tableName
WHERE LearnerId = 1
于 2013-02-05T11:46:40.020 回答