我有四张桌子User,Destination,City and visited
如下
declare @user table
(
UserId int identity(1,1) not null ,
UserName nvarchar(500)
)
insert into @user (UserName) values ('rahul')
insert into @user (UserName) values ('nitin')
insert into @user (UserName) values ('yunus')
declare @destination table
(
destCode nvarchar(50),
destName nvarchar(500)
)
insert into @destination select 'SWDP','ranthambore national park '
insert into @destination select 'BTP','ghana national park '
declare @city table
(
cityId int identity(1,1) not null,
cityname nvarchar(500)
)
insert into @city select 'jaipur'
insert into @city select 'delhi'
-- visited table in which user id with either destCode or cityId
declare @visited table
(
UserId int ,
LocationFrom nvarchar(500),
LocationTo nvarchar(500),
LocType nvarchar(50)
)
insert into @visited select '1','BTP','1','city'
insert into @visited select '1','1','SWDP','dest'
insert into @visited select '2','1','2','city'
insert into @visited select '3','2','SWDP','dest'
insert into @visited select '3','SWDP','BTP','dest'
select * from @user
select * from @destination
select * from @city
select * from @visited
select * from @visited as v
inner join @user as u ON v.UserId=u.UserId
访问表中的位置类型列表示LocationTo
类型。它的城市或位置。
现在我想加入这些表,这样我就可以获得用户从哪个城市或目的地开始并去哪个城市和目的地的数据。
预期结果如下图
这仅适用于一个用户,但我需要所有用户都相同。