msaql,两个表操作的,之后初始化的问题。
表1:StaNum_ZDZ_Rec_all 3个字段a b c
表2:Template_ZDZ_Z_Rec 2个字段a d
两个表的字段a完全相同
表2的d字段全部为“0”
从表1中查询字段b=“sx” 字段c=“正常”和“缺报” 的字段a
然后将表2中a与 表1查询结果相同的行的字段d置为1
以上操作完成后将表2初始化 即所有d置为“0”
不知道表达的是否清楚...请大家帮忙。
[解决办法]
- SQL code
update 2set d=1where a in (select a from 1)goupdate 1set d=0
[解决办法]
- SQL code
update t2set d=1from Template_ZDZ_Z_Rec t2join StaNum_ZDZ_Rec_all t1 on t2.a=t1.awhere t1.b='sx' and t1.c in('正常','缺报')
[解决办法]
- SQL code
--"正常”和“缺报 另一个意思为同时包含正常”和“缺报update t2set d=1from Template_ZDZ_Z_Rec t2where exists(select 1 from StaNum_ZDZ_Rec_all where a.=t2.a and c='正常')and exists(select 1 from StaNum_ZDZ_Rec_all where a.=t2.a and c='缺报')
[解决办法]
- SQL code
update a set a.d=1from Template_ZDZ_Z_Rec a join StaNum_ZDZ_Rec_all b on a.a=b.awhere b.b='sx' and b.c in ('正常','缺报')
[解决办法]
直接查不匹配的就是了
select y.a,y.d from t2 y
left join t1 x
on x.a=y.a
where x.b='sx' and charindex(x.c,'正常缺报')>0
and x.a isnull
[解决办法]
- SQL code
create table StaNum_ZDZ_Rec_all(a站号 int, b日期 int,c报文状态 varchar(50))insert into StaNum_ZDZ_Rec_all select 50953 , 2007 , '正常' insert into StaNum_ZDZ_Rec_all select 50850 , 2007 , '缺报' insert into StaNum_ZDZ_Rec_all select 50772 , 2007 , '缺报' insert into StaNum_ZDZ_Rec_all select 50742 , 2007 , '正常' insert into StaNum_ZDZ_Rec_all select 50765 , 2007 , '好' create table Template_ZDZ_Z_Rec (a站号 int, d接收状态 int) insert into Template_ZDZ_Z_Rec select 50953 , 0 insert into Template_ZDZ_Z_Rec select 50850 , 0 insert into Template_ZDZ_Z_Rec select 50772 , 0 insert into Template_ZDZ_Z_Rec select 50742 , 0 insert into Template_ZDZ_Z_Rec select 50986 , 0 insert into Template_ZDZ_Z_Rec select 50812 , 0 select b.a站号 from Template_ZDZ_Z_Rec b where a站号 not in(select a站号 from StaNum_ZDZ_Rec_all b1 where b1.c报文状态 in ('正常','缺报'))