大虾们看看我的SQL,
select m.id, m.title, m.createdate, m.publisher, m.publishername,count(t.id) as replyCounts
from epare_safe_callboard m left join epare_safeboard_supplyment  t on m.id=t.safeboradid, epare_safe_callboard_view v  
where  (validdate-sysdate)>=-1  and m.id=v.SAFEBOARDID and v.userid= 4107 and v.VIEWFLAG=0  
group by m.id, m.title, m.createdate, m.publisher, m.publishername order by 1 desc union select m.id, m.title, m.createdate, m.publisher, m.publishername,count(t.id) as replyCounts
from epare_safe_callboard m left join epare_safeboard_supplyment  t on m.id=t.safeboradid  
where  (validdate-sysdate)>=-1   and m.DEPTCID= '九部' 
group by m.id, m.title, m.createdate, m.publisher, m.publishername order by 1 desc
我做了测试.2个SQL是可以运行的,为什么加了union就不行了,总是报命令未正确结束,这是怎么回事啊?

解决方案 »

  1.   

    给出 epare_safe_callboard 和epare_safeboard_supplyment 的建表DDL
      

  2.   

    和group by 无关
    union前的 order by 1 desc 要去掉
      

  3.   

    select m.id, m.title, m.createdate, m.publisher, m.publishername,count(t.id) as replyCounts
    from epare_safe_callboard m left join epare_safeboard_supplyment t on m.id=t.safeboradid, epare_safe_callboard_view v 
    where (validdate-sysdate)>=-1 and m.id=v.SAFEBOARDID and v.userid= 4107 and v.VIEWFLAG=0 
    group by m.id, m.title, m.createdate, m.publisher, m.publishername 
    union 
    select m.id, m.title, m.createdate, m.publisher, m.publishername,count(t.id) as replyCounts
    from epare_safe_callboard m left join epare_safeboard_supplyment t on m.id=t.safeboradid 
    where (validdate-sysdate)>=-1 and m.DEPTCID= '九部' 
    group by m.id, m.title, m.createdate, m.publisher, m.publishername 
    order by 1 desc