I have the following table where I sort by prioridad
and id_agenda
, the order should be as follows Priority ASC and id_agenda
DESC (In case a data does not have priority so it must go below the if it has priority).
sql
CREATE TABLE IF NOT EXISTS 'ACT_Agenda' (
'id_agenda' int(11) NOT NULL AUTO_INCREMENT,
'prioridad' int(11) DEFAULT NULL,
'actividad' text,
PRIMARY KEY ('id_agenda')
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=83 ;
INSERT INTO 'ACT_Agenda' ('id_agenda', 'prioridad', 'actividad') VALUES
(51, 1, 'act1'),
(60, 2, 'act2'),
(62, 3, 'act3'),
(70, NULL, 'act4'),
(71, NULL, 'act5'),
Here is my Code where I order.
$sql = "SELECT * FROM ACT_Agenda as a
ORDER BY prioridad DESC, id_agenda DESC ";
My result. sqlFiddle
---------------------------
|id |prioridad| actividad|
----|---------|-----------|
|62 | 3 |act1 | <--Debería ordenar por la primera prioridad
|60 | 2 |act2 |
|51 | 1 |act3 |
|71 | |act5 | <--Ultimo valor ingresado
|70 | |act4 |
|---|---------|-----------|
But if I sort by ASC priority, the table gets messed up leaving the last id
first. I would like to have a result like that.
---------------------------
|id |prioridad| actividad|
----|---------|-----------|
|51 | 1 |act1 | <--Ordena por primera prioridad
|60 | 2 |act2 |
|62 | 3 |act3 |
|71 | |act5 | <--Ultimo valor ingresado
|70 | |act4 |
|---|---------|-----------|