php - Mysql表达式按日期描述从数据库中选择所有内容

标签 php mysql

我有以下保留作业的数据库:

user_job(job_id, job_title, day, month, year)。 day, month, year 字段包含每个作业的到期日期。日持有值 1..31,月持有值 1 月...12 月和 2014 年...2040 年。

我想从这个表中选择 * 按到期日期 DESC 排序。知道如何编写我的表达式吗?我的意思是我想在到期日 DESC 之前找到工作。 这是我试过但没有用的方法:

<?php

  $get_jobs = mysql_query("select * FROM `user_job` order by `day`, `month`, `year` DESC ");

?>

最佳答案

select * FROM `user_job` 
order by `year` desc, 
          case when `month` = 'january' then 1 
               when `month` = 'february' then 2
               when `month` = 'march' then 3
               when `month` = 'april' then 4
               when `month` = 'may' then 5
               when `month` = 'june' then 6
               when `month` = 'july' then 7
               when `month` = 'august' then 8
               when `month` = 'september' then 9
               when `month` = 'october' then 10
               when `month` = 'november' then 11
               when `month` = 'december' then 12
          end desc,
         `day` desc

关于php - Mysql表达式按日期描述从数据库中选择所有内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24365010/

相关文章:

mysql - SQL 查询中的语法错误无法理解

PHP 优化 : Fetching from database

php - 为什么是 "hash(' md 5', ' string')"faster than "md5 ('string' )"?

javascript - 在 php 中使用 mysql 枚举和 javascript 显示和隐藏元素?

mysql - 如何按列值 "user_id"和 "gps_time"对mysql进行分区?

php - mysqli:它可以在一条语句中准备多个查询吗?

php - Jquery/AJAX 更新链接/按钮上的表

php - 如何在不循环查询的情况下获取类别树?

php - 使用 xpath 时避免类中的某些元素

php - "Unfolding"一个字符串