2014-12-27 84 views
0

我有這兩個查詢。在MySQL中的聯合查詢

select * from blog_post where isDelete=0 
      and pOrder !=0 order by pOrder asc 
select * from blog_post where isDelete=0 
      and pOrder=0 order by postID desc 

這工作正常。

但是當我嘗試加入這個兩個查詢到union

select * from 
    ((select * from blog_post 
       where isDelete=0 and pOrder !=0 
       order by pOrder asc) 
     union all 
    (select * from blog_post 
       where isDelete=0 and pOrder=0 
       order by postID desc)) as tbl1 

它給第二個查詢的升序輸出,但在查詢它是按降序排列。

+1

在你的查詢pOrder和postID是不同的列..? – Hrithu

+0

是它的不同列 –

回答

0

您可以嘗試如下。

(select * from blog_post where isDelete=0 and pOrder !=0 order by pOrder asc) 
UNION 
(select * from blog_post where isDelete=0 and pOrder=0 order by postID desc) 
+0

它不工作,我試過這 –

0

嘗試這樣

select * from(select col1,col2,...colN,'1' as sorting from blog_post where isDelete=0 and pOrder !=0 order by pOrder asc UNION All select col1,col2,...colN,'1' as sorting from blog_post where isDelete=0 and pOrder=0 order by postID desc) temp order by sorting asc,if (sorting=1,pOrder,0) asc,if (sorting=2,postID,0) desc 
+0

它不工作 –

0

你可以試試這個:

select * from blog_post where isDelete=0 and pOrder != 0 
UNION ALL 
select * from blog_post where isDelete=0 and pOrder = 0 
order by pOrder,postID 
+0

它給了錯誤的輸出。 –

+0

你的意思是錯誤的output.order或data。 –

0

我通過以下解決了這個查詢。

SELECT * FROM blog_post其中isDelete = 0 ORDER BY(案例pOrder那麼當0(選擇馬克斯(pOrder)+ 1從blog_post)其他pOrder完)遞增,帖子ID遞減

感謝對這個問題的評論,我只是在努力,上面的查詢給了我完美的我想要的。