“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
换句话说,如何使所有当前帖子在查询中排在第一位?
最佳答案
为每个联合部分创建一个虚拟列 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
括号很重要。还要确保每个并集部分中的列数和顺序相同。
关于mysql - 如何优先考虑工会中未过期的职位,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34780104/