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

标签 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

换句话说,如何使所有当前帖子在查询中排在第一位?

最佳答案

为每个联合部分创建一个虚拟列 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/

相关文章:

mysql - 使用带有 Asterisk 的 mysql

mysql - 如何计算过期日期 3个月前就过期了

mysql - 如何将 UCS-2 Little Endian 编码的文件上传到 UTF-8 格式的 Mysql 表中

mysql - 查询没有返回数据

mysql - mac安装后无法使用mysql命令

mysql - 具有相同数据的两个 MYSQL 表具有不同的大小

java - 无法解析 serviceImpl 类中的 'ST_GeomFromText'

php - 外键约束失败

php - 当表为空时 Yii 查询不返回 null

php - 如何在mysql中为图像设置标签/关键字