27,580
社区成员
发帖
与我相关
我的任务
分享
select top 10 * from (
select *,row_number() over(order by Aid desc) as row from A
inner join B on A.Bid=B.Bid where B.BName like '%key%'
union all
select *,row_number() over(order by Aid desc) as row from A
inner join C on A.Cid=C.Cid where C.CName like '%key%'
) as t
where row between 1 and 10
order by Aid desc
select top 10 * from (
select *,row_number() over(order by Aid desc) as row from A
inner join B on A.Bid=B.Bid
inner join C on A.Cid=C.Cid
where C.CName like '%key%' or B.BName like '%key%'
) as t
where row between 1 and 10
order by Aid desc
select top 10 * from (
select *,row_number() over(order by Aid desc) as row from A
inner join B on A.Bid=B.Bid
inner join C on A.Cid=C.Cid
where C.CName like '%key%' or B.BName like '%key%'
) as t
where row between 1 and 10
order by Aid desc
if object_id('aa') is not null drop table aa
create table aa(aid char(10),bid char(10),cid char(10))
if object_id('bb') is not null drop table bb
create table bb(bid char(10),bname char(30))
if object_id('cc') is not null drop table cc
create table cc(cid char(10),cname char(30))
insert into aa
select '1','1','1' union all
select '2','2','3' union all
select '3','4','1' union all
select '4','2','2' union all
select '5','3','1' union all
select '6','1','4' union all
select '7','1','5'
select * from aa
insert into bb
select '4','ddd' union all
select '2','bbb' union all
select '3','ccc'
update bb set bname='bkey' where bid=2 or bid=3
insert into cc
select '1','caaa' union all
select '2','cbbb' union all
select '3','cccc' union all
select '4','cddd'
update cc set cname='ckey' where cid=1 or cid=4
with bbs as
(
select a.aid,b.bid,b.bname,c.cid,c.cname from aa a join bb b on a.bid=b.bid join cc c on a.cid=c.cid
)
select * from bbs a where exists(select 1 from bbs where a.aid=bbs.aid and a.bname like '%key%' or a.cname like '%key%')
/*
aid bid bname cid cname
1 1 aaa 1 ckey
6 1 aaa 4 ckey
2 2 ckey 3 cccc
4 2 ckey 2 cbbb
5 3 ckey 1 ckey
3 4 ddd 1 ckey
*/