[英]Retrieve single record with recent date from a table having same column value in multiple records
[英]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
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.