Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to sort twice in SQL with time and boolean

This is my table

CREATE TABLE `matches` (
    `no` int NOT NULL AUTO_INCREMENT,
    `matchID` varchar(128) NOT NULL,
    `teamOne` varchar(128) NOT NULL,
    `teamTwo` varchar(128) NOT NULL,
    `schedule` TIMESTAMP NOT NULL,
    `isVisible` BOOLEAN NOT NULL,
    `isDone` BOOLEAN NOT NULL,
    `isCancelled` BOOLEAN NOT NULL,
    `isStarted` BOOLEAN NOT NULL,
    PRIMARY KEY (`no`)
);

And this is my current query

SELECT * FROM matches WHERE isVisible = 1 ORDER BY schedule ASC

it’s working correctly and sorted according to the schedule.
But I want to make all the isDone records in the bottom while still sorting according to the schedule. Here is an example output that I want:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

+-----------+----------+--------+
| matchID   | schedule | isDone |
+-----------+----------+--------+
| example10 | 04:00    | 0      |
+-----------+----------+--------+
| example40 | 05:00    | 0      |
+-----------+----------+--------+
| example65 | 06:00    | 0      |
+-----------+----------+--------+
| example42 | 07:00    | 0      |
+-----------+----------+--------+
| example13 | 02:00    | 1      |
+-----------+----------+--------+
| example69 | 03:00    | 1      |
+-----------+----------+--------+
| example50 | 03:30    | 1      |
+-----------+----------+--------+

thanks

>Solution :

You can place multiple columns inside ORDER BY

SELECT * FROM matches WHERE isVisible = 1 ORDER BY isDone, schedule ASC
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading