3

这是演示

订单是 11 月和 10 月。我怎样才能将 ORDER BY 添加到此,以便按月、1 月、2 月、3 月...等顺序排列。

提前致谢。

CREATE TABLE `hw_homework` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `studentid` int(10) NOT NULL,
  `subjectid` int(10) NOT NULL,
  `assignment_name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `teacherid` int(10) NOT NULL,
  `date` date NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=55 ;

--
-- Dumping data for table `hw_homework`
--

INSERT INTO `hw_homework` (`id`, `studentid`, `subjectid`, `assignment_name`, `teacherid`, `date`) VALUES
(52, 56, 13, '1A', 20, '2012-10-28'),
(53, 56, 6, '12', 18, '2012-10-28'),
(54, 56, 4, 'page42', 59, '2012-11-02');


SELECT  studentID, 
        DATE_FORMAT(`date`,'%M') `month`,
        COUNT(studentid) totalMissed
FROM hw_homework
WHERE studentid = 56
GROUP BY studentid, DATE_FORMAT(`date`, '%M')
4

7 回答 7

8

正如Chuidian建议的那样,我添加了以下内容以获得我想要的。感谢大家。

ORDER BY Month(date)
于 2012-11-02T06:32:47.740 回答
2

在您的情况下,它是根据 aplhabets 订购的。以下将给出正确的结果。

SELECT  studentID, 
    DATE_FORMAT(`date`,'%M') `month`,
    COUNT(studentid) totalMissed
FROM hw_homework
WHERE studentid = 56
GROUP BY studentid, DATE_FORMAT(`date`, '%M')
ORDER BY DATE_FORMAT(`date`,'%m')
于 2012-11-02T06:57:11.037 回答
1

在您的查询中使用如下

GROUP BY MONTH(date)
于 2012-11-02T05:50:39.373 回答
1

偶然发现这篇文章试图找到按月份名称排序的解决方案。可能不是很优雅的解决方案,但您可以使用FIELD字符串函数来获取该结果集。

SELECT
studentID,
DATE_FORMAT(`date`, '%M') `month`,
COUNT(studentid) totalMissed FROM
hw_homework WHERE
studentid = 56 GROUP BY
studentid, 
DATE_FORMAT(`date`, '%M') ORDER BY
FIELD(
    DATE_FORMAT(`date`, '%M'),
    'January',
    'February',
    'March',
    'April',
    'May',
    'June',
    'July',
    'August',
    'September',
    'October',
    'November',
    'December'
);

试试看。date出于可读性目的,您也可以将 DATE_FORMAT 替换为更简单的函数 MONTHNAME( )。

于 2018-03-01T23:48:40.147 回答
0
 SELECT  studentID, 
    DATE_FORMAT(`date`,'%M') as `month`,
    COUNT(studentid) totalMissed
FROM hw_homework
WHERE studentid = 56
order by Month(month)
GROUP BY studentid, DATE_FORMAT(`date`, '%M')
于 2012-11-02T05:49:44.430 回答
0
SELECT  studentID, 
        DATE_FORMAT(`date`,'%M') `month`,
        COUNT(studentid) totalMissed
FROM hw_homework
WHERE studentid = 56
GROUP BY MONTH(DATE) ASC
于 2012-11-02T05:53:24.843 回答
-1

mysql> select * from hw_homework order by date asc; //递增顺序

或者

mysql> select * from hw_homework order by date desc;

于 2012-11-02T05:51:08.670 回答