如CSDN网友给的SQLselect a.* from Module a,Temp b where a.ID=b.ID and a.Type='页面'
union
select m.* from Module m
where 
    m.Type='按钮'
    and
    not exists(select 1 from Temp where ID=m.ID)
    and
    exists(select 1 from Module c,Temp d where c.ID=d.ID and c.Type='页面' and c.PageURL=m.PageURL)
然后我套到我的地方就是select a.* from Module a,(..... 这里表示很长的一般查询) b where a.ID=b.ID and a.Type='页面'
union
select m.* from Module m
where 
    m.Type='按钮'
    and
    not exists(select 1 from Temp where ID=m.ID)
    and
    exists(select 1 from Module c,(..... 这里表示很长的一般查询) d where c.ID=d.ID and c.Type='页面' and c.PageURL=m.PageURL)如上
因为上面那个
(..... 这里表示很长的一般查询)
这里面的查询是一样的
如果像上面这样的话就是要进行两次查询出一样的数据
如何能句一次就OK呢谢谢

解决方案 »

  1.   

    (..... 这里表示很长的一般查询) 
    把这个查询放入一临时表(TEMP),然后使用临时表即可.如果非要一个查询语句完成,就需要使用两次这个同样的查询了.
      

  2.   

    所以建议你把查询结果放入一临时表(TEMP).
    然后:select a.* from Module a,Temp b where a.ID=b.ID and a.Type='页面'
    union
    select m.* from Module m
    where 
        m.Type='按钮'
        and
        not exists(select 1 from Temp where ID=m.ID)
        and
        exists(select 1 from Module c,Temp d where c.ID=d.ID and c.Type='页面' and c.PageURL=m.PageURL)如果你这个查询不麻烦的话,记录少的话可以:select a.* from Module a,(..... 这里表示很长的一般查询) b where a.ID=b.ID and a.Type='页面'
    union
    select m.* from Module m
    where 
        m.Type='按钮'
        and
        not exists(select 1 from (..... 这里表示很长的一般查询) t where ID=m.ID)
        and
        exists(select 1 from Module c,(..... 这里表示很长的一般查询) d where c.ID=d.ID and c.Type='页面' and c.PageURL=m.PageURL)