如何在工会中优先考虑未过期的职位

时间:2016-01-14 01:18:56

标签: mysql

“offer”类型将以1或0到期,“Sweep”类型将在到期日期到期。如果查询未过期,我如何在查询中给予他们更大的权重?

当前查询(针对搜索页面):

    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

换句话说,如何在查询中首先发布所有当前帖子?

1 个答案:

答案 0 :(得分: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

括号很重要。还要确保每个联合部分中列的数量和顺序是相同的。