我有以下2个表格:
A)表格
继
id fkUserName follow_fkUserName
用户配置
id fkUserName Name Surname
B)我想做什么
我想按照以下方式加入这两个表:
id User_fkUserName User_Name User_Surname Followed_fkUserName Followed_Name Followed_Surname
C)我的问题:
我的问题是,当我尝试通过视图加入时,我没有得到预期的结果。
我希望最后我会得到这个结果:
Id User_fkUsername User_Name User_Surname Followed_fkUserName Followed_Name Followed_Surname 1 pbean Paul Bean mWhite Mary White
D)我正在使用的代码:
以下是我用于加入的代码:
CREATE VIEW [dbo].[vwFollowingUsers] AS SELECT a.Id, b.Name User_Name, c.Surname User_Surname, d.Name Followed_Name, e.Surname Followed_Surname FROM Following a LEFT JOIN UserProfile b ON a.fkUserName = b.fkUserName LEFT JOIN UserProfile c ON a.fkUserName = c.fkUserName LEFT JOIN UserProfile d ON a.follow_fkUserName = d.fkUserName LEFT JOIN UserProfile e ON a.follow_fkUserName = e.fkUserName
E)这就是我得到的:
vwFollowingUsers
Id User_Name Followed_Name 1 Paul Mary
我正在关注此question
F)更新代码
在我发布问题后,我已根据以下内容更新了我的代码:
CREATE VIEW [dbo].[vwFollowingUsers] AS SELECT A.Id, B.fkUserName AS User_fkUserName, B.Name AS User_Name, B.Surname AS User_Surname, C.fkUserName AS Followed_fkUsername, C.Name AS Followed_Name, C.Surname AS Followed_Surname FROM Following A INNER JOIN UserProfile B ON A.fkUserName = B.fkUserName INNER JOIN UserProfile C ON A.follow_fkUserName = C.fkUserName;
G)实际数据:
这是实际数据(按要求)
继
Id fkUsername follow_fkUserName 2 nic@gmail.com steph@gmail.com
用户配置
Id UserPhoto Description fkUserName Name Surname 1 ../Files/nich.jpg test nich@gmail.com Nich White 2 ../Files/nich2.jpg null nich2@gmail.com Nich2 White2 3 ../Files/steph.jpg null steph@gmail.com Stephania Borg
我正在使用Visual Studio,我正在尝试创建一个视图,它会在我的问题的G部分中提供结果。
答案 0 :(得分:1)
您只需加入 UserProfile 2次,即为用户提取数据,为追随者提取另一项数据。
试试这个:
org.apache.spark.sql.Row = [United Kingdom,{"nm": "Cnut","cty": "United Kingdom","hse": "House of Denmark","yrs": "1016-1035"},Edmund lronside,1016]
答案 1 :(得分:1)
此处不需要4个联接 - 您只想从UserProfile
中提取两个行。
您可以简单地执行以下操作:
SELECT f.Id,
up1.fkUserName User_fkUsername,
up1.Name User_Name,
up1.Surname User_Surname,
up2.fkUserName Followed_fkUsername,
up2.Name Followed_Name,
up2.Surname Followed_Surname
FROM Following f
INNER JOIN UserProfile up1 ON f.fkUserName = up1.fkUserName
INNER JOIN UserProfile up2 ON f.fkUserName = up2.fkUserName