如何使用order by按当前日期排序?

时间:2017-08-04 07:09:02

标签: mysql

这是我的代码,我试图根据当前日期对结果进行排序以获取数据。

  

我需要的是让jb_expiry_date更接近当前日期的日期

    SELECT vm_jobs_category.*
           , vm_jobs.* 
           , DATE_FORMAT(vm_jobs.jb_expiry_date,'%Y%m%d') AS pub
           , vm_seo_detail.* 
           , vm_publications.* 
      FROM vm_jobs 
INNER JOIN vm_jobs_category 
        ON (vm_jobs.jb_jc_id = vm_jobs_category.jc_id ) 
INNER JOIN vm_seo_detail 
        ON (vm_jobs.jb_id = vm_seo_detail.sd_ty_id AND vm_seo_detail.sd_ty = 'vm_jobs' ) 
INNER JOIN vm_publications 
        ON (vm_jobs.jb_id = vm_publications.ty_id AND vm_publications.ty_name = 'vm_jobs' AND vm_publications.status = '1')
     WHERE (vm_jobs.jb_expiry_date != '' AND vm_jobs.jb_expiry_date != '1970-01-01' AND vm_jobs.jb_expiry_date != '0000-00-00') 
     ORDER BY pub DESC

1 个答案:

答案 0 :(得分:0)

只需计算当前日期的差异(以适合您的任何单位)并按此顺序。

...
ORDER BY TIMESTAMPDIFF(DAY, vm_jobs.jb_expiry_date, NOW())
  • here关于此功能的文档