2016-02-24 139 views
0

我的SQL查詢有問題。Mysql與ORDER查詢UNION不起作用

此代碼用於顯示有發佈日期的文章。

例如:

BLOG ONE: day of pubblication every 16th of mounth 
BLOG TWO: day of pubblication every 2nd of mounth 
BLOG THREE: day of pubblication every 23th of mounth 

所以今天我24日必須看到博客與出版24,25,26,27,28,1,2,3,4,5的一天....... ..........

問題是合併兩個查詢

(DAY FROM TODAY TO 31th) + (DAY FROM 1st TO YESTERDAY) 

這是第一個查詢

$queryallblog = mysql_query(" 
(
    SELECT * 
    FROM day,blog 
    WHERE day.autore = blog.titolo && day.titolo >= DAY(CURRENT_DATE()) 
    ORDER BY day.titolo 
)") or die(" Error: (" . mysql_errno() . ") " . mysql_error()); 

,這是第二個:

$queryallblog = mysql_query(" 
(
    SELECT * 
    FROM day,blog 
    WHERE day.autore = blog.titolo && day.titolo < DAY(CURRENT_DATE()) 
    ORDER BY day.titolo 
)") or die(" Error: (" . mysql_errno() . ") " . mysql_error()); 

使用UNION語句的全球秩序是不正確的

+0

爲什麼jquery標籤? – Jens

+0

你在哪裏查詢Union? – Jens

+0

向我們展示您的'UNION ALL'聲明! – jarlh

回答

0
(SELECT * 

FROM day,blog 
WHERE day.autore = blog.titolo && day.titolo >= DAY(CURRENT_DATE()) 
ORDER BY day.titolo) 
UNION ALL 
(SELECT * 

FROM day,blog 
WHERE day.autore = blog.titolo && day.titolo < DAY(CURRENT_DATE()) 
ORDER BY day.titolo) 
1

如果我正確理解你的問題,你的問題是不是一個UNION查詢,但它是關於正確排序結果。我會用這樣的查詢:

SELECT 
    * 
FROM 
    day inner join blog on day.autore = blog.titolo 
ORDER BY 
    case when day.titolo >= DAY(CURRENT_DATE()) then 1 else 2 end, 
    day.titolo 
  • 我使用的是內部聯接,而不是其中的語法
  • 情況下將返回1時day.titolo爲> =當天,和2除此以外。首先將在當天之後返回行,然後是當前日期之前的行
  • 然後我按天排序行。您可能需要添加其他過濾器。本月

您可以獲得與UNION ALL查詢了類似的結果,但你必須添加一個額外的列:

SELECT 1 as t, * 
FROM day inner join blog on day.autore = blog.titolo 
WHERE day.titolo >= DAY(CURRENT_DATE()) 
UNION ALL 
SELECT 2 as t, * 
FROM day inner join blog on day.autore = blog.titolo 
WHERE day.titolo < DAY(CURRENT_DATE()) 
ORDER BY t, day.titolo 

但是我不會reccomend它,至少不會在這個背景下。