--drop table #T1
--drop table #T2
create Table #T1(ID int,
QueryID nvarchar(20),
ResultID1 nvarchar(20),
ResultID2 nvarchar(20))
create Table #T2(SortNo int,
QueryID nvarchar(20),
ResultID1 nvarchar(20),
ResultID2 nvarchar(20))
insert into #T1(ID,QueryID)
select 1,N'Account'
union select 2,N'Bill'
insert into #T2(SortNo,QueryID,ResultID1,ResultID2)
select 1,'A%','A1','A1'
union select 2,'B%','B2','B2'
union select 3,'Ac%','A2','A2'
select * from #T1
select * from #T2
/*
--需要通过一条SQL语句,根据T1的QueryID LIKE T2.QueryID 来找到ResultID1和ResultID2,并把这两个值更新到T1表中,但是要按SortNo的倒序来筛选值
--得到的结果要求是:
ID QueryID,ResultID1,ResultID2
1 Account A2 A2
2 Bill B2 B2
*/
SQL code:
--drop table #T1
--drop table #T2
create Table #T1(ID int,
QueryID nvarchar(20),
ResultID1 nvarchar(20),
ResultID2 nvarchar(20))
create Table #T2(SortNo int,
QueryID nvarchar(20),
ResultID1 nvarchar(20),
ResultID2 nvarchar(20))
insert into #T1(ID,QueryID)
select 1,N'Account'
union select 2,N'Bill'
insert into #T2(SortNo,QueryID,ResultID1,ResultID2)
select 1,'A%','A1','A1'
union select 2,'B%','B2','B2'
union select 3,'Ac%','A2','A2'
upd