--追加测试数据
insert into tabel2 select 1, 1, 1
insert into tabel2 select 1, 2, 1
insert into tabel2 select 1, 3, 1
insert into tabel2 select 2, 1, 1
insert into tabel2 select 2, 2, 0
insert into tabel2 select 2, 3, 0
--你要求的SQL语句
select *
from tabel2 as T
where w3=1 and exists (select * from tabel2 where l1=T.l1 and w2=T.w2+1 and w3=0)
union
select *
from tabel2 as T
where w3=0 and exists (select * from tabel2 where l1=T.l1 and w2=T.w2-1 and w3=1)
--你要求的SQL语句
select *
from tabel2 as T
where w3=1 and exists (select * from tabel2 where l1=T.l1 and w2=T.w2+1 and w3=0)
union
select *
from tabel2 as T
where w3=0 and exists (select * from tabel2 where l1=T.l1 and w2=T.w2-1 and w3=1)