SQL 一条记录的的两个字段值相同与不同的查询

2025-03-23 02:26:02
推荐回答(3个)
回答1:

create table #tb
(
xm varchar(20),
je int,
xm01 varchar(20),
flag int 
)
go
insert #tb
select '岳恒勇',2401,'刘良福',0 union all
select '刘良福',2833,'岳恒勇',1 union all
select '曹庆波',2489,'曹庆波',0 union all
select '崔世全',2407,'崔世全',1
go
select * from #tb
where exists(select 1 from #tb b where #tb.xm=b.xm01 and #tb.je<>b.je)
select * from #tb
/*
xm je xm01 flag
------------------------------------
岳恒勇 2401 刘良福 1
刘良福 2833 岳恒勇 0
*/

回答2:

楼主如下写即可:

UPDATE TABLE SET FLAG = 1 WHERE XM =XM01 AND je <> je01

回答3:

select * from (select xm,je from table) a , (select xm01,je01 from table) b
where a.xm = b.xm01
and a.je <> b.je01