clear markdown compare help best fiddles feedback
clear markdown feedback
By using db<>fiddle, you agree to license everything you submit by Creative Commons CC0. 3601472 fiddles created (47958 in the last week).

SELECT VERSION();
VERSION()
10.2.6-MariaDB-10.2.6+maria~jessie-log
 hidden batch(es)


DROP TABLE IF EXISTS `GEAR`;
 hidden batch(es)


CREATE TABLE IF NOT EXISTS `GEAR` ( `stat` INT UNSIGNED NOT NULL, `quality` VARCHAR(7) NOT NULL );
 hidden batch(es)


INSERT INTO `GEAR` (`stat`, `quality`) VALUES (1, 'normal'), (11, 'normal'), (12, 'better'), (2, 'better');
 hidden batch(es)


SET @`bool` := FALSE;
 hidden batch(es)


PREPARE `stmt` FROM CONCAT(' SELECT `stat`, `quality` FROM `GEAR` ORDER BY ', CASE WHEN TRUE = @`bool` THEN '`stat`' WHEN FALSE = @`bool` THEN '`quality`' END, ' DESC');
 hidden batch(es)


EXECUTE `stmt`;
stat quality
1 normal
11 normal
12 better
2 better
 hidden batch(es)


SET @`bool` := TRUE;
 hidden batch(es)


PREPARE `stmt` FROM CONCAT(' SELECT `stat`, `quality` FROM `GEAR` ORDER BY ', CASE WHEN TRUE = @`bool` THEN '`stat`' WHEN FALSE = @`bool` THEN '`quality`' END, ' DESC');
 hidden batch(es)


EXECUTE `stmt`;
stat quality
12 better
11 normal
2 better
1 normal
 hidden batch(es)


DEALLOCATE PREPARE `stmt`;
 hidden batch(es)