加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > MsSql教程 > 正文

根据多条件查询临时表 想得到不同结果集的方法

发布时间:2020-12-12 09:34:09 所属栏目:MsSql教程 来源:网络整理
导读:当我写下如下sql语句时,我得到了输入@c参数时想得到的结果集。 select from @tb t where t.id in (select id from tb where f = @c) 但如果有@a,@b,@c,而它们分别想从@tb中得到不同的结果集,例如 div class="codetitle" a style="CURSOR: pointer" data="

当我写下如下sql语句时,我得到了输入@c参数时想得到的结果集。
select from @tb t where t.id in (select id from tb where f = @c)
但如果有@a,@b,@c,而它们分别想从@tb中得到不同的结果集,例如
<div class="codetitle"><a style="CURSOR: pointer" data="34762" class="copybut" id="copybut34762" onclick="doCopy('code34762')"> 代码如下:<div class="codebody" id="code34762">
if @a is not null
begin
--得到@a想得到的
end
if @b is not null
begin
--得到@b想得到的
end
if @c is not null
begin
--得到@c想得到的
end

这样做好像没什么问题,但如果@a和@b是一起的,甚至是@a,@c,@d,@e,@f等等N多种条件组合,这样就不好办了。所以必须先build好@tb,最后一次性查询
--构造@tb
select
from @tb
假如我已经通过@a,@b得到了一种@tb结果集,当我再次使用@c进行条件判断时,这样就会覆盖刚才的结果。
可以采用“删除不符合条件的记录”的方法来做,由于@tb已经得到了@a,@b想得到的结果,所以只要删除掉不符合@c的结果就行了。完。
<div class="codetitle"><a style="CURSOR: pointer" data="26476" class="copybut" id="copybut26476" onclick="doCopy('code26476')"> 代码如下:<div class="codebody" id="code26476">
if @c is not null
begin
delete c from @tb c where c.id not in (select id from tb where f = @c)
end
select * from @tb

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读