mssql 同表多次查询用一条SQL语句怎么写
假设有表[str],字段id,tag以我这个菜鸟的水平以下用三次条件运行第一次:select*fromstrwheretaglike'%条件一%'andtaglike'%...
假设有表 [str] ,字段 id,tag 以我这个菜鸟的水平以下用三次条件运行
第一次:select * from str where tag like '%条件一%' and tag like '%条件二%' and tag like '%条件三%'
第二次:select * from str where tag like '%条件一%' and tag like '%条件二%'
第三次:select * from str where tag like '%条件一%'
怎么用一条SQL语句书写?并让查询的结果从条件多的到条件少的这样的顺序排列? 展开
第一次:select * from str where tag like '%条件一%' and tag like '%条件二%' and tag like '%条件三%'
第二次:select * from str where tag like '%条件一%' and tag like '%条件二%'
第三次:select * from str where tag like '%条件一%'
怎么用一条SQL语句书写?并让查询的结果从条件多的到条件少的这样的顺序排列? 展开
展开全部
select a.*,b.match from (
select id,
(case
when (tag like '%条件一%' and tag like '%条件二%' and tag like '%条件三%') then 3
when (tag like '%条件一%' and tag like '%条件二%') then 2
when (tag like '%条件一%') then 1
else 0
end) as match
from [str]
) b join [str] a on a.id=b.id
where b.match>0
order by b.match desc
select id,
(case
when (tag like '%条件一%' and tag like '%条件二%' and tag like '%条件三%') then 3
when (tag like '%条件一%' and tag like '%条件二%') then 2
when (tag like '%条件一%') then 1
else 0
end) as match
from [str]
) b join [str] a on a.id=b.id
where b.match>0
order by b.match desc
追问
这种写法,刚试了可行。请问这种写法相比较我的笨方法,那种速度更快,更节约资源?
追答
其实照你的思路,可以像这样写在一条SQL语句里:
select * from str where tag like '%条件一%' and tag like '%条件二%' and tag like '%条件三%'
union
select * from str where tag like '%条件一%' and tag like '%条件二%' and tag not like '%条件三%'
union
select * from str where tag like '%条件一%' and tag not like '%条件二%'
至于说两种方法的性能对比,我个人感觉是差不多的...
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
create procedure sp_Search @条件1 nvarchar(150),@条件2 nvarchar(150),@条件3 nvarchar(150)
declare @sql nvarcahr(4000)
set @sql='select * from str where 1=1 '
if len(@条件1)>0
begin
set @sql=@sql+' and tag like '%'+@条件1+'% '
end
if len(@条件2)>0
begin
set @sql=@sql+' and tag like '%'+@条件2+'% '
end
if len(@条件3)>0
begin
set @sql=@sql+' and tag like '%'+@条件3+'% '
end
exec @sql
declare @sql nvarcahr(4000)
set @sql='select * from str where 1=1 '
if len(@条件1)>0
begin
set @sql=@sql+' and tag like '%'+@条件1+'% '
end
if len(@条件2)>0
begin
set @sql=@sql+' and tag like '%'+@条件2+'% '
end
if len(@条件3)>0
begin
set @sql=@sql+' and tag like '%'+@条件3+'% '
end
exec @sql
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
select * from str where (tag like '%条件一%' and tag like '%条件二%' and tag like '%条件三%')
or (tag like '%条件一%' and tag like '%条件二%')
or (tag like '%条件一%')
order by len(tag) - len(replace(tag,'条件','')) desc
or (tag like '%条件一%' and tag like '%条件二%')
or (tag like '%条件一%')
order by len(tag) - len(replace(tag,'条件','')) desc
更多追问追答
追问
order by len(tag) - len(replace(tag,'条件','')) desc 这里的条件是什么?
追答
并让查询的结果从条件多的到条件少的这样的顺序排列?
你不是说从条件多到条件少么?这个是啥意思?
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
另外一种写法:
select * from str
where (tag like '%条件一%' and tag like '%条件二%' and tag like '%条件三%')
or (tag like '%条件一%' and tag like '%条件二%')
or (tag like '%条件一%')
order by case when tag like '%条件三%' then 0 else 1 end,
case when tag like '%条件二%' then 0 else 1 end
select * from str
where (tag like '%条件一%' and tag like '%条件二%' and tag like '%条件三%')
or (tag like '%条件一%' and tag like '%条件二%')
or (tag like '%条件一%')
order by case when tag like '%条件三%' then 0 else 1 end,
case when tag like '%条件二%' then 0 else 1 end
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询