2016-01-14 67 views
0

「要約」類型的期限爲1或0,「清算」類型的截止日期爲截止日期。當他們沒有過期時,我怎樣才能在查詢中給予他們更多的權重?如何優先考慮工會中未過期的帖子

(搜索頁面)的當前查詢:

SELECT 
    'offer' AS type, 
    id, 
    expired, 
    title, 
    description, 
    header, 
    slug, 
    date_original, 
    date 
FROM cs_offers 
WHERE MATCH(title,title,description,keywords,header,country,offer_description) AGAINST ('$search_main' IN BOOLEAN MODE) 

    UNION 

SELECT 
    'sweep' AS type, 
    id, 
    title, 
    description, 
    header, 
    expire_date, 
    slug, 
    date_original, 
    date_original AS date 
FROM cs_sweeps 
WHERE MATCH(title,title,description,keywords,header,category,location,entry_type,country,w) AGAINST ('$search_main' IN BOOLEAN MODE) 

    UNION 

SELECT 
    'video' AS type, 
    id, 
    id AS id2, 
    title, 
    description, 
    keywords, 
    slug, 
    date_original, 
    date 
FROM cs_vlog 
WHERE MATCH(title,title,description,keywords,video_description) AGAINST ('$search_main' IN BOOLEAN MODE) 


    UNION 

SELECT 
    'post' AS type, 
    id, 
    id AS id2, 
    title, 
    description, 
    header, 
    slug, 
    date_original, 
    date 
FROM cs_blog 
WHERE MATCH(title,title,description,keywords,header,body) AGAINST ('$search_main' IN BOOLEAN MODE) 
ORDER BY date DESC 

換句話說,如何才能讓當前所有帖子首先在查詢?

回答

3

對於每一個工會的一部分創建一個虛擬列expired,然後通過order by expired asc, date desc訂購您的整個聯盟:

(select ..., expired from offer ...) 
union all 
(select ..., expire_date < now() as expired from sweep ...) 
... 
order by expired asc, date desc 

括號是非常重要的。還要確保每個聯合部分的列數和順序是相同的。

+0

您的回答不明確。如果一個過期1或0,另一個過期 - 如何比較結果? – DaedBaet

+1

答案非常清楚。正如你所看到的,你只需通過'expire_date

+0

@DaedBaet,將其轉換爲相同的'格式'。例如,如果你的表有一個'expired'列,其中'0'表示有效項,'1'表示過期,你可以按原樣使用它,因此在select中使用'expired'。如果你有一個項目的過期日期,你可以使用'expire_date weirdan