如何获取包含公司 (ID) 的公司列表?
How do I get a list of corporations where corporation (ID) is included?
重要...在我的 RL 项目中,ID 不是 INT,而是 GUID,所以我的数据不是分层的!
我有一个关于公司的 table 和一个关于公司之间链接的 table。
我需要能够从特定公司 ID 检索公司列表。
这是我的测试代码...
CREATE TABLE ##corporations
(
CorporationID INT NOT NULL,
CorporationName NVARCHAR(20) NOT NULL
);
CREATE TABLE ##corporationLinks
(
FromCorporationID INT NOT NULL,
ToCorporationID INT NOT NULL
);
INSERT INTO ##corporations (CorporationID, CorporationName) VALUES (1, 'Nike')
INSERT INTO ##corporations (CorporationID, CorporationName) VALUES (2, 'Cocal Cola')
INSERT INTO ##corporations (CorporationID, CorporationName) VALUES (3, 'Apple')
INSERT INTO ##corporations (CorporationID, CorporationName) VALUES (4, 'Google')
INSERT INTO ##corporations (CorporationID, CorporationName) VALUES (5, 'Amazon')
INSERT INTO ##corporations (CorporationID, CorporationName) VALUES (6, 'Samsung')
INSERT INTO ##corporationLinks (FromCorporationID, ToCorporationID) VALUES (1, 2)
INSERT INTO ##corporationLinks (FromCorporationID, ToCorporationID) VALUES (2, 3)
INSERT INTO ##corporationLinks (FromCorporationID, ToCorporationID) VALUES (4, 5)
INSERT INTO ##corporationLinks (FromCorporationID, ToCorporationID) VALUES (4, 6)
SELECT * FROM ##corporationLinks WHERE FromCorporationID = 2 OR ToCorporationID = 2
/**
Organisations (##corporationLinks) are...
Nike + Coca Cola + Apple + Marcy
and...
Google + Amazon + Samsung
**/
-- How do I eg get a list of companies where Coca Cola is in ... that is where FromCorporationID = 2 OR ToCorporationID = 2 ... result should be Nike, Coca Cola and Apple?
-- How do I eg get a list of companies where Samsung is in ... that is where FromCorporationID = 6 OR ToCorporationID = 6 ... result should be Google, Amazon Cola and Samsung?
DROP TABLE ##corporationLinks
DROP TABLE ##corporations
更新:
如果我需要找到可口可乐所属的公司,那么我会去寻找...
SELECT * FROM ##corporationLinks WHERE FromCorporationID = 2 OR ToCorporationID = 2
然后我会得到2个结果...
FromCorporationID ToCorporationID
-----------------------------------
1 2
2 3
在此之后,我需要查看结果属于哪些公司...
SELECT * FROM ##corporationLinks WHERE FromCorporationID = 1 OR ToCorporationID = 1
SELECT * FROM ##corporationLinks WHERE FromCorporationID = 3 OR ToCorporationID = 3
那我再拿一个法团(7):
FromCorporationID ToCorporationID
-----------------------------------
3 7
然后我需要深入了解与 7 关联的公司...
SELECT * FROM ##corporationLinks WHERE FromCorporationID = 7 OR ToCorporationID = 7
并深入研究该结果(猜测它被称为递归)。等等
更新 2:
我已经更新了上面的示例,添加了一个公司,如果搜索的是可口可乐,则应返回该公司。
上述查询 (Coca Cola) 的预期结果:
CorporationID:
--------------
2
1
3
7
左加入##companies,包括其 CorporationID,或者您可以在查询中包括公司名称,然后按 CorporationID 分组,您应该能够看到它属于哪个公司
用递归CTE
:
with
root as (select CorporationID id from companies where CorporationName = 'Coca Cola'),
cte as (
select
case when r.id = cl.FromCorporationID then cl.ToCorporationID else cl.FromCorporationID end id,
case when r.id = cl.FromCorporationID then cl.FromCorporationID else cl.ToCorporationID end otherid
from corporationLinks cl inner join root r
on r.id in (cl.FromCorporationID, cl.ToCorporationID)
union all
select
case when c.id = cl.FromCorporationID then cl.ToCorporationID else cl.FromCorporationID end id,
case when c.id = cl.FromCorporationID then cl.FromCorporationID else cl.ToCorporationID end otherid
from corporationLinks cl inner join cte c
on c.id in (cl.FromCorporationID, cl.ToCorporationID)
and c.otherid not in (cl.FromCorporationID, cl.ToCorporationID)
)
select id CorporationID from root
union all
select id from cte
参见demo。
结果:
> | CorporationID |
> | ------------: |
> | 2 |
> | 1 |
> | 3 |
> | 7 |
只需使用递归 CTE
WITH cte
AS
(
SELECT l1.FromCorporationID,l1.ToCorporationID
FROM ##corporationLinks l1
UNION ALL
SELECT cte.FromCorporationID,l3.ToCorporationID
FROM ##corporationLinks l3
JOIN cte
ON cte.ToCorporationID = l3.FromCorporationID
)
SELECT cte.ToCorporationID--parent
FROM cte
WHERE cte.FromCorporationID =2
UNION
SELECT cte.FromCorporationID--child
FROM cte
WHERE cte.ToCorporationID =2
UNION
SELECT c.CorporationID--self
FROM ##corporations c
WHERE c.CorporationID = 2
重要...在我的 RL 项目中,ID 不是 INT,而是 GUID,所以我的数据不是分层的!
我有一个关于公司的 table 和一个关于公司之间链接的 table。
我需要能够从特定公司 ID 检索公司列表。
这是我的测试代码...
CREATE TABLE ##corporations
(
CorporationID INT NOT NULL,
CorporationName NVARCHAR(20) NOT NULL
);
CREATE TABLE ##corporationLinks
(
FromCorporationID INT NOT NULL,
ToCorporationID INT NOT NULL
);
INSERT INTO ##corporations (CorporationID, CorporationName) VALUES (1, 'Nike')
INSERT INTO ##corporations (CorporationID, CorporationName) VALUES (2, 'Cocal Cola')
INSERT INTO ##corporations (CorporationID, CorporationName) VALUES (3, 'Apple')
INSERT INTO ##corporations (CorporationID, CorporationName) VALUES (4, 'Google')
INSERT INTO ##corporations (CorporationID, CorporationName) VALUES (5, 'Amazon')
INSERT INTO ##corporations (CorporationID, CorporationName) VALUES (6, 'Samsung')
INSERT INTO ##corporationLinks (FromCorporationID, ToCorporationID) VALUES (1, 2)
INSERT INTO ##corporationLinks (FromCorporationID, ToCorporationID) VALUES (2, 3)
INSERT INTO ##corporationLinks (FromCorporationID, ToCorporationID) VALUES (4, 5)
INSERT INTO ##corporationLinks (FromCorporationID, ToCorporationID) VALUES (4, 6)
SELECT * FROM ##corporationLinks WHERE FromCorporationID = 2 OR ToCorporationID = 2
/**
Organisations (##corporationLinks) are...
Nike + Coca Cola + Apple + Marcy
and...
Google + Amazon + Samsung
**/
-- How do I eg get a list of companies where Coca Cola is in ... that is where FromCorporationID = 2 OR ToCorporationID = 2 ... result should be Nike, Coca Cola and Apple?
-- How do I eg get a list of companies where Samsung is in ... that is where FromCorporationID = 6 OR ToCorporationID = 6 ... result should be Google, Amazon Cola and Samsung?
DROP TABLE ##corporationLinks
DROP TABLE ##corporations
更新:
如果我需要找到可口可乐所属的公司,那么我会去寻找...
SELECT * FROM ##corporationLinks WHERE FromCorporationID = 2 OR ToCorporationID = 2
然后我会得到2个结果...
FromCorporationID ToCorporationID
-----------------------------------
1 2
2 3
在此之后,我需要查看结果属于哪些公司...
SELECT * FROM ##corporationLinks WHERE FromCorporationID = 1 OR ToCorporationID = 1
SELECT * FROM ##corporationLinks WHERE FromCorporationID = 3 OR ToCorporationID = 3
那我再拿一个法团(7):
FromCorporationID ToCorporationID
-----------------------------------
3 7
然后我需要深入了解与 7 关联的公司...
SELECT * FROM ##corporationLinks WHERE FromCorporationID = 7 OR ToCorporationID = 7
并深入研究该结果(猜测它被称为递归)。等等
更新 2:
我已经更新了上面的示例,添加了一个公司,如果搜索的是可口可乐,则应返回该公司。
上述查询 (Coca Cola) 的预期结果:
CorporationID:
--------------
2
1
3
7
左加入##companies,包括其 CorporationID,或者您可以在查询中包括公司名称,然后按 CorporationID 分组,您应该能够看到它属于哪个公司
用递归CTE
:
with
root as (select CorporationID id from companies where CorporationName = 'Coca Cola'),
cte as (
select
case when r.id = cl.FromCorporationID then cl.ToCorporationID else cl.FromCorporationID end id,
case when r.id = cl.FromCorporationID then cl.FromCorporationID else cl.ToCorporationID end otherid
from corporationLinks cl inner join root r
on r.id in (cl.FromCorporationID, cl.ToCorporationID)
union all
select
case when c.id = cl.FromCorporationID then cl.ToCorporationID else cl.FromCorporationID end id,
case when c.id = cl.FromCorporationID then cl.FromCorporationID else cl.ToCorporationID end otherid
from corporationLinks cl inner join cte c
on c.id in (cl.FromCorporationID, cl.ToCorporationID)
and c.otherid not in (cl.FromCorporationID, cl.ToCorporationID)
)
select id CorporationID from root
union all
select id from cte
参见demo。
结果:
> | CorporationID |
> | ------------: |
> | 2 |
> | 1 |
> | 3 |
> | 7 |
只需使用递归 CTE
WITH cte
AS
(
SELECT l1.FromCorporationID,l1.ToCorporationID
FROM ##corporationLinks l1
UNION ALL
SELECT cte.FromCorporationID,l3.ToCorporationID
FROM ##corporationLinks l3
JOIN cte
ON cte.ToCorporationID = l3.FromCorporationID
)
SELECT cte.ToCorporationID--parent
FROM cte
WHERE cte.FromCorporationID =2
UNION
SELECT cte.FromCorporationID--child
FROM cte
WHERE cte.ToCorporationID =2
UNION
SELECT c.CorporationID--self
FROM ##corporations c
WHERE c.CorporationID = 2