-1

例如,我的数据库中有月份名称。tomonthfrommonth。存储的数据如“JAN”、“FEB”等。

我该怎么做才能得到tomonth 和之间的区别frommonth

4

2 回答 2

0

试试下面的查询

select (tomonth-frommonth) as monthdiff from
( SELECT case when `frommonth` = 'Jan' then 1 
   when frommonth = 'Feb' then 2  
   when frommonth = 'Mar' then 3 
   when frommonth = 'Apr' then 4  
   when frommonth = 'May' then 5  
   when frommonth = 'Jun' then 6  
   when frommonth = 'Jul' then 7  
   when frommonth = 'Aug' then 8  
   when frommonth = 'Sep' then 9  
   when frommonth = 'Oct' then 10  
   when frommonth = 'Nov' then 11  
   when frommonth = 'Dec' then 12 
   end frommonth, 
( case when tomonth = 'Jan' then 1
   when tomonth = 'Feb' then 2  
   when tomonth = 'Mar' then 3 
   when tomonth = 'Apr' then 4  
   when tomonth = 'May' then 5  
   when tomonth = 'Jun' then 6  
   when tomonth = 'Jul' then 7  
   when tomonth = 'Aug' then 8  
   when tomonth = 'Sep' then 9  
   when tomonth = 'Oct' then 10  
   when tomonth = 'Nov' then 11  
   when tomonth = 'Dec' then 12 
   end ) as tomonth from table) a
于 2013-03-18T09:40:33.780 回答
0

您可以使用 STR_TO_DATE:

SELECT
  MONTH(STR_TO_DATE(tomonth,'%b'))-MONTH(STR_TO_DATE(frommonth,'%b')) as montdiff
FROM
  yourtable
于 2013-03-18T09:57:43.383 回答