![](/img/trans.png)
[英]Updating a Column with the minimum value of that same column depending on a 2nd column
[英]Find values in one column that share same value in different column without knowing the 2nd cols value
鑒於此表:
Relationship
managerId companyId
12 33
19 33
27 44
21 33
4 20
有沒有辦法找到共享同一家公司的所有managerId但只知道一個managerId並且不知道companyId
例如,如果我們只知道managerId是12
SELECT companyId
FROM Relationship
WHERE managerId = 12
我們顯然會得到33
回。 但是在同一個查詢中有一種方法可以取回所有managerId,其中companyId是第一個語句的返回值。 所以在這種情況下,只要知道managerId = 12我就想回到12,19,21
。
在companyId
上將表連接到自己:
select b.managerId
from relationship a
join relationship b on b.companyId = a.companyId
where a.managerId = 19
這是一種方法:
select r.*
from relationship r
where r.companyid = (select r2.companyid from relationship r2 where r2.managerid = 12);
注意:此特定方法假定relationship.managerid
是唯一的。 鑒於您的問題和樣本數據,這似乎是合理的。
試試這個:
select managerId from relationship
where companyId = select companyId from relationship where managerId = 12;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.