表a productid ljbh mbjs tmlj
101 101-01-01 50 101-01-01$101-01-02
101 101-01-02 50 101-01-01$101-01-02
101 101-01-05 80 101-01-05$101-01-06
101 101-01-06 80 101-01-05$101-01-06
最后要得到的表
productid ljbh mbjs tmlj
101 101-01-01 50 101-01-01$101-01-02
101 101-01-02 0 101-01-01$101-01-02
101 101-01-05 80 101-01-05$101-01-06
101 101-01-06 0 101-01-05$101-01-06
我只是让最最终用户看到下面的结果,真正数据库中并没有更新,请问我该如何做到!~
declare @t table(productid int, ljbh varchar(20), mbjs int, tmlj varchar(30))
insert @t
select 101 , '101-01-01', 50 , '101-01-01$101-01-02' union all
select 101 , '101-01-02', 50 , '101-01-01$101-01-02' union all
select 101 , '101-01-05', 80 , '101-01-05$101-01-06' union all
select 101 , '101-01-06', 80 , '101-01-05$101-01-06'select productid,
ljbh,
mbjs=case when exists(select 1 from @t where productid=a.productid and tmlj = a.tmlj and ljbh>a.ljbh) then mbjs else 0 end,
tmlj
from @t a
后边的查询语句什么意思那位能给我详细解释一下啊尤其上里面的case when....then esle end什么意思,我是新手大家多帮忙谢谢
101 101-01-01 50 101-01-01$101-01-02
101 101-01-02 50 101-01-01$101-01-02
101 101-01-05 80 101-01-05$101-01-06
101 101-01-06 80 101-01-05$101-01-06
最后要得到的表
productid ljbh mbjs tmlj
101 101-01-01 50 101-01-01$101-01-02
101 101-01-02 0 101-01-01$101-01-02
101 101-01-05 80 101-01-05$101-01-06
101 101-01-06 0 101-01-05$101-01-06
我只是让最最终用户看到下面的结果,真正数据库中并没有更新,请问我该如何做到!~
declare @t table(productid int, ljbh varchar(20), mbjs int, tmlj varchar(30))
insert @t
select 101 , '101-01-01', 50 , '101-01-01$101-01-02' union all
select 101 , '101-01-02', 50 , '101-01-01$101-01-02' union all
select 101 , '101-01-05', 80 , '101-01-05$101-01-06' union all
select 101 , '101-01-06', 80 , '101-01-05$101-01-06'select productid,
ljbh,
mbjs=case when exists(select 1 from @t where productid=a.productid and tmlj = a.tmlj and ljbh>a.ljbh) then mbjs else 0 end,
tmlj
from @t a
后边的查询语句什么意思那位能给我详细解释一下啊尤其上里面的case when....then esle end什么意思,我是新手大家多帮忙谢谢
-------------
SQL的控制流语句 相当于 IF ELSE 结构!
用法可参看SQL联机丛书的该结构的语法和示例
then ……2
else ……3
end
是条件判断语句,当条件1成立,就执行2,否则执行3。