I want to sort my database result based on a column value. I thought I needed to do this:
SELECT * FROM products
ORDER BY FIELD(brand_id, 4, 1, 6)
But this doesn't sort my result at all. Or at least in a way I don't understand.
What I want to achieve is that I get all products, ordered by brand_id. First all with brand_id 4, then 1 then 6 and then the rest.
Here is my example on jsfiddle.
If not working (jsfiddle not working fine here), see my code below:
Schema:
CREATE TABLE `products` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`brand_id` int(10) unsigned NOT NULL,
`price` decimal(8,2) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB;
INSERT INTO `products` (`id`, `brand_id`, `price`)
VALUES
(1, 1, 10.32),
(2, 4, 15.32),
(3, 2, 23.32),
(4, 6, 56.32),
(5, 4, 23.32),
(6, 4, 23.32),
(7, 1, 25.32),
(8, 5, 15.32),
(9, 3, 55.32),
(10, 5, 23.32);