62,046
社区成员
发帖
与我相关
我的任务
分享
--> 测试数据: @a
declare @a table (年龄 int,身高 int,学历 varchar(1))
insert into @a
select 12,170,'A' union all
select 11,150,'B' union all
select 10,160,'C'
--> 测试数据: @b
declare @b table (年龄1 int,年龄2 int,身高1 int,身高2 int,学历 varchar(1))
insert into @b
select 12,13,140,145,'B' union all
select 11,13,170,180,'a' union all
select 12,13,170,180,'A'
select a.* from @A a
where exists(select 1 from @b b where (年龄 between b.年龄1 and b.年龄2) or (身高 between b.身高1 and b.身高2) or b.学历=a.学历)
order by
(case when exists(select 1 from @b b where 年龄 between b.年龄1 and b.年龄2) then 1 else 0 end)+
(case when exists(select 1 from @b b where 身高 between b.身高1 and b.身高2) then 1 else 0 end)+
(case when exists(select 1 from @b b where b.学历=a.学历) then 1 else 0 end)
desc
--符号错了。
select a.* from a,b
where 年龄 between b.年龄1 and b.年龄2 or 身高 between b.身高1 and b.身高2 or b.学历=a.学历
order by
(case when (年龄 between b.年龄1 and b.年龄2) then 1 else 0 end)+
(case when (身高 between b.身高1 and b.身高2) then 1 else 0 end)+
(case when a.学历=b.学历 then 1 else 0 end)
desc
select a.* from a left join b
where 年龄 between b.年龄1 and b.年龄2 or 身高 between b.身高1 and b.身高2 or b.学历=a.学历)
order by
(case when (年龄 between b.年龄1 and b.年龄2) then 1 else 0 end)+
(case when (身高 between b.身高1 and b.身高2) then 1 else 0 end)+
(case when a.学历=b.学历 then 1 else 0 end)
desc