原数据是
id name parent_id quesnum
1 aa null null
2 bb 1 0
3 cc 1 119
4 dd null null
5 ee 4 0
6 ff 5 30
7 gg 5 40
8 qq 5 null要得到以下结果
id name parent_id quesnum
1 aa null null
3 cc 1 119
4 dd null null
5 ee 4 0
6 ff 5 30
7 gg 5 40
就是如果是两层的,第二层不要。三层的,第二层的只要第三有数据不管有没有数据都要,第三层没有数据不要
id name parent_id quesnum
1 aa null null
2 bb 1 0
3 cc 1 119
4 dd null null
5 ee 4 0
6 ff 5 30
7 gg 5 40
8 qq 5 null要得到以下结果
id name parent_id quesnum
1 aa null null
3 cc 1 119
4 dd null null
5 ee 4 0
6 ff 5 30
7 gg 5 40
就是如果是两层的,第二层不要。三层的,第二层的只要第三有数据不管有没有数据都要,第三层没有数据不要
where exists (
select 1 from tablename
where parent_id=a.id
)
or isnull(quesnum,0)<>0
quesnum=(case when a.quesnum=0 then null else a.quesnum end ),b.state from (select a.* from user_worktype a
where exists (select 1 from user_worktype where parent_id=a.id)
or isnull(quesnum,0)<>0 ) a ,user_worktype_quespower b
where a.id = b.worktype_id