我在SQL Server中使用图表。
这是我的桌子:
--Node Table
CREATE TABLE [dbo].[Users]
(
[ID] [int] NOT NULL Primary key,
[FName] [nvarchar](100) NULL,
[LName] [nvarchar](100) NULL
)AS NODE
--Edge Table
CREATE TABLE [dbo].[FriendsOf] AS EDGE
Run Code Online (Sandbox Code Playgroud)
如何选择User2的所有User1朋友和User2是User3的朋友,而User1和User3之间没有直接Edge关系。
我可以这样写这个查询:
select distinct
u1.FName + ' ' + u1.LName as FirstFullName,
u2.FName + ' ' + u2.LName as SecondFullName,
u3.FName + ' ' + u3.LName as ThirdFullName
from
Users u1, FriendsOf fo1, Users u2, FriendsOf fo2, Users u3
where
match(u1-(fo1)->u2-(fo2)->u3)
and not exists(select 1 from friendsof fof …Run Code Online (Sandbox Code Playgroud)