0

我在 MYSQL 中有一个表,其中在其他字段中我有两个字段,一个用于月(Varchar)和年(int)。月份字段用于存储月份的名称,年份用于存储输入数据的年份。我很困惑如何使用 where 类只显示最大月份和年份的记录。例如

10 records are there for month January 2013
50 records are there for month February 2013
.
.
.
100 records are there for the month of November 2014 --- (LAST ENTRY)

现在在这里我只想显示 2014 年 11 月的记录。我的代码是这样编写的,我不能使用选择查询,我必须使用 WHERE CLAUSE

4

3 回答 3

1
select count(*), year, month
from your_table
group by year, month
order by year,
         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
limit 1
于 2013-10-25T09:39:59.190 回答
1
SELECT *
FROM tablename
WHERE
  (year, month) = (SELECT year, month
                   FROM tablename
                   ORDER BY
                     STR_TO_DATE(CONCAT_WS(' ', '01', month, year), '%d %M %Y') DESC
                   LIMIT 1)
于 2013-10-25T09:48:23.250 回答
0

尝试这个

select Count(*),month,year from tablename 
group by STR_TO_DATE(CONCAT_WS(' ', '01', month, year), '%d %M %Y') order by
STR_TO_DATE(CONCAT_WS(' ', '01', month, year), '%d %M %Y')
于 2013-10-25T09:51:23.080 回答