22,209
社区成员
发帖
与我相关
我的任务
分享
declare @tb table (sequenceno int,locationname nvarchar(10))
insert into @tb select 1,'beijing'
union all select 2,'shanghai'
union all select 3,'tianjin'
select a.locationname [from] ,b.locationname [to] from @tb a join @tb b
on a.sequenceno=b.sequenceno-1
from to
---------- ----------
beijing shanghai
shanghai tianjin
(2 行受影响)
create table tb(sequenceNo int,LocationName varchar(8))
insert tb
select 1,'beijing' union all
select 2,'shanghai' union all
select 3,'tianjin'
select
LocationName as [from],
(select LocationName from tb where sequenceNo=t.sequenceNo+1) as [to]
from tb t
from to
-------- --------
beijing shanghai
shanghai tianjin
tianjin NULL
(3 行受影响)
create table [tb]([sequenceNo] int,[LocationName] varchar(8))
insert [tb]
select 1,'beijing' union all
select 2,'shanghai' union all
select 3,'tianjin'
go
select m.[LocationName] , n.[LocationName] from tb m , tb n where m.[sequenceNo] = n.[sequenceNo] - 1
drop table tb
/*
LocationName LocationName
------------ ------------
beijing shanghai
shanghai tianjin
(所影响的行数为 2 行)
*/
create table tb
(
sequenceNo int,
LocationName nvarchar(50)
)
insert tb select 1,'beijing'
insert tb select 2,'shanghai'
insert tb select 3,'tianjin'
create view fromto
as
select a.LocationName as [from], b.LocationName as [to] from tb a inner join tb b on a.sequenceNo=b.sequenceNo-1
go
select * from fromto
drop view fromto
drop table tb
from to
-------------------------------------------------- --------------------------------------------------
beijing shanghai
shanghai tianjin
(2 個資料列受到影響)
---测试数据---
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([sequenceNo] int,[LocationName] varchar(8))
insert [tb]
select 1,'beijing' union all
select 2,'shanghai' union all
select 3,'tianjin'
---定义视图---
create view View_tb
as
select *
from
(
select
LocationName as [from],
(select LocationName from tb where sequenceNo=t.sequenceNo+1) as [to]
from tb t
) tt where [to] is not null
---查询---
select * from View_tb
---结果---
from to
-------- --------
beijing shanghai
shanghai tianjin
(所影响的行数为 2 行)
declare @t table(sequenceNo int, LocationName varchar(20) )
insert @t select 1, 'beijing'
insert @t select 2, 'shanghai'
insert @t select 3, 'tianjin'
select a.LocationName [from],b.[LocationName] [to] from @t a join @t b on a.sequenceNo=b.sequenceNo-1
from to
-------------------- --------------------
beijing shanghai
shanghai tianjin
(所影响的行数为 2 行)
---测试数据---
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([sequenceNo] int,[LocationName] varchar(8))
insert [tb]
select 1,'beijing' union all
select 2,'shanghai' union all
select 3,'tianjin'
---查询---
select
LocationName as [from],
(select LocationName from tb where sequenceNo=t.sequenceNo+1) as [to]
from tb t
---结果---
from to
-------- --------
beijing shanghai
shanghai tianjin
tianjin NULL
(所影响的行数为 3 行)