590
社区成员
发帖
与我相关
我的任务
分享
drop table if exists `house`;
create table `house`(room_detail varchar(100),room_name varchar(100),subdistrict_id int,id int,`show` int,delflag int);
drop table if exists `subdistrict`;
create table `subdistrict`(id int, subdistrict_name varchar(10));
drop table if exists `contract`;
create table `contract`(id int, house_id int);
insert into `subdistrict`(id,subdistrict_name)values(1,'S1'),(2,'S2');
insert into `house`(room_detail,room_name,subdistrict_id,id,`show`)values('01','1CON',1,1,0),('01','1CON',1,2,0),('01','NOCON',1,3,0),('01','NOCON',1,4,0)
,('02',null,2,5,0),('02',null,2,6,0),('05','BB',2,7,0),('07','2con',2,8,0),('07','2con',2,9,0);
insert into `contract`(id,house_id)values(1,1),(2,9),(3,5),(4,6);
select h.*,seq,HasContract,case when p.seq>1 then 1 else 0 end as DeleteFlag from
`house` h inner join (
SELECT t.id
, HasContract
,case when t.room_detail=@lastrd and @lastrn=ifnull(t.room_name,'') then @i:=@i+1 else @i:=1 end as Seq
,@lastrd:=t.room_detail as v_room_detail,@lastrn:=ifnull(t.room_name,'') as v_room_name
from (
SELECT h.room_detail,h.room_name,h.subdistrict_id,h.id ,case when c.id is null then 0 else 1 end as HasContract
FROM `house` as h
left join `contract` as c on c.house_id=h.id
where `show` <> 1
) as t
,(select @i:=1,@lastrd:='',@lastrn:='') x
order by t.room_detail,t.room_name,t.HasContract desc
) p on p.id=h.id;
##where p.seq>1;
room_detail room_name subdistrict_id id show delflag seq HasContract DeleteFlag
1 01 1CON 1 1 0 NULL 1 1 0
2 01 1CON 1 2 0 NULL 2 0 1
3 01 NOCON 1 3 0 NULL 1 0 0
4 01 NOCON 1 4 0 NULL 2 0 1
5 02 NULL 2 5 0 NULL 1 1 0
6 02 NULL 2 6 0 NULL 2 1 1
7 05 BB 2 7 0 NULL 1 0 0
8 07 2con 2 8 0 NULL 2 0 1
9 07 2con 2 9 0 NULL 1 1 0