我在MYSQL中有一个表,其中我有两个字段,分别是Month(Varchar)和year(int)。 Months字段用于存储月份名称,year用于存储输入数据的年份。我很困惑,如何使用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月的记录。我的代码以这样的方式编写,我不能使用select查询,我必须使用WHERE CLAUSE
答案 0 :(得分: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
答案 1 :(得分: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)
答案 2 :(得分: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')