Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to sort by month in MySQL

Here is demo

The order is November and October. How can I add ORDER BY to this, so that the order is by month, Jan, Feb, Mar...etc.

Thanks in advance.

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')
like image 945
shin Avatar asked Oct 29 '25 09:10

shin


1 Answers

As Chuidiang suggested I added the following to get what I wanted. Thanks everyone.

ORDER BY Month(date)
like image 166
shin Avatar answered Oct 31 '25 00:10

shin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!