日期:2014-05-16  浏览次数:20426 次

exist union 运用求大中小级联
所有item表中,大,中,小item.
其中大的item是W039,中的事W002,小的事W003.
然后item的betogroup指向上一级item的grou.
求所有大的item和它所属的中,小item.

select * from item smallitem where itemtype='W003' and exists
(
  select '1' from item middleitem where middleitem.itemtype='W002' and smallitem.betogroup= middleitem.group and exists
  (
    select '1' from item largeitem where largeitem.itemtype='W039' and middleitem.betogroup= largeitem.itemtype.group
  )
)
union
select * from item middleitem where itemtype='W002' and exists
(select '1' from item largeitem where largeitem.itemtype='W039' and middleitem.betogroup= largeitem.group)
union
select * from item largeitem where largeitem.itemtype='W039'