Du kan bruge COALESCE
:
ORDER BY COALESCE(dtModified, dtPosted)
En anden mulighed er at bruge den MySQL-specifikke funktion IFNULL
i stedet for COALESCE
.
Test på MySQL:
CREATE TABLE table1 (dtModified DATETIME NULL, dtPosted DATETIME NOT NULL);
INSERT INTO table1 (dtModified, dtPosted) VALUES
('2010-07-31 10:00:00', '2010-07-30 10:00:00'),
(NULL , '2010-07-31 09:00:00'),
('2010-07-31 08:00:00', '2010-07-30 10:00:00');
SELECT dtModified, dtPosted
FROM table1
ORDER BY COALESCE(dtModified, dtPosted)
Resultater:
dtModified dtPosted
2010-07-31 08:00:00 2010-07-30 10:00:00
NULL 2010-07-31 09:00:00
2010-07-31 10:00:00 2010-07-30 10:00:00