Home > Mobile >  How to sort twice in SQL with time and boolean
How to sort twice in SQL with time and boolean

Time:11-23

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:

 ----------- ---------- -------- 
| 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

CodePudding user response:

You can place multiple columns inside ORDER BY

SELECT * FROM matches WHERE isVisible = 1 ORDER BY isDone, schedule ASC
  •  Tags:  
  • sql
  • Related