用caseselect a.字段1,a.字段2,a.字段3,
case isnull(a.字段4,'') when '' then (select 字段4 from tttt b where b.字段1=2 and b.字段2=a.字段2 and b.字段3=a.字段3) else a.字段4 end
from tttt a order by 字段1,字段2,字段3楼主的要求还没有明确,如果在字段1=2的里面没有符合条件的,将取谁请楼主在明确一下,
case isnull(a.字段4,'') when '' then (select 字段4 from tttt b where b.字段1=2 and b.字段2=a.字段2 and b.字段3=a.字段3) else a.字段4 end
from tttt a order by 字段1,字段2,字段3楼主的要求还没有明确,如果在字段1=2的里面没有符合条件的,将取谁请楼主在明确一下,
case isnull(a.字段4,'') when '' then (select 字段4 from tttt b where b.字段1=2 and b.字段2=a.字段2 and b.字段3=a.字段3) else a.字段4 end
from tttt a where 字段1=1 order by 字段1,字段2,字段3
字段1=1表示一级菜单,字段1=2表示二级菜单。
字段2表示二级菜单分类,
字段3表示菜单顺序如果一级菜单(也就是字段1=1)的某一项(比如字段2=0)无默认菜单(也就是字段4=null),那么就取相应二级菜单(字段2=0)中顺序号(字段3)最小的(也就是字段3=1)字段1 字段2 字段3 字段4
1 0 1 null
1 1 2 大
1 2 3 null
1 3 4 小 2 0 1 不大
2 0 2 ll
2 0 3 klkl
2 1 1 不小
2 2 2 jkjk
2 3 1 中
要求得到
1 0 1 不大
1 1 2 大
1 2 3 不小
1 3 4 小字段1=1时,字段4中有可能无数据
字段1=2时,字段4中总有数据要查询得字段1=1的数据。
查询时:
字段1=1时的字段4数据优先,为空的话取字段1=2时的字段4中数据
case isnull(a.字段4,'') when '' then (select 字段4 from tttt b where b.字段1=2 and b.字段2=a.字段2 and b.字段3=a.字段3) else a.字段4 end
from tttt a where 字段1=1 order by 字段1,字段2,字段3