lundi 27 octobre 2014

Mysql: order by with fallback


Vote count:

0




I want to retrieve data with a simple SELECT statement and ORDER BY, but the ORDER BY should have a fallback.


That is: my records have a filed name and a field name sortkey:



CREATE TABLE company
(`id` int, `name` varchar(10), `sortkey` varchar(10))
;

INSERT INTO company
(`id`, `name`, `sortkey`)
VALUES
(1, 'Azzz', ''),
(2, 'Bzzz', ''),
(3, 'Czzz', 'Aaa')
;


I'd like to get the result from a SELECT statement:



Czzz
Azzz
Bzzz


When I have a sortkey, use it. If not, use the field name.


Peudo code: SELECT * FROM company ORDER BY name,sortkey (but prioritize the sortkey);



asked 2 mins ago

topskip

2,874






Mysql: order by with fallback

Aucun commentaire:

Enregistrer un commentaire