Mega Code Archive

 
Categories / MySQL / Select Clause
 

To sort by product category, extract the category value and use it in the ORDER BY clause

mysql> mysql> CREATE TABLE housewares     -> (     ->  id                      VARCHAR(20),     ->  description     VARCHAR(255)     -> ); Query OK, 0 rows affected (0.00 sec) mysql> mysql> INSERT INTO housewares (id,description)     ->  VALUES     ->          ('DIN40672US', 'dining table'),     ->          ('KIT00372UK', 'garbage disposal'),     ->          ('KIT01729JP', 'microwave oven'),     ->          ('BED00038SG', 'bedside lamp'),     ->          ('BTH00485US', 'shower stall'),     ->          ('BTH00415JP', 'lavatory')     -> ; Query OK, 6 rows affected (0.00 sec) Records: 6  Duplicates: 0  Warnings: 0 mysql> mysql> SELECT * FROM housewares; +------------+------------------+ | id         | description      | +------------+------------------+ | DIN40672US | dining table     | | KIT00372UK | garbage disposal | | KIT01729JP | microwave oven   | | BED00038SG | bedside lamp     | | BTH00485US | shower stall     | | BTH00415JP | lavatory         | +------------+------------------+ 6 rows in set (0.00 sec) mysql> mysql> SELECT * FROM housewares ORDER BY LEFT(id,3); +------------+------------------+ | id         | description      | +------------+------------------+ | BED00038SG | bedside lamp     | | BTH00485US | shower stall     | | BTH00415JP | lavatory         | | DIN40672US | dining table     | | KIT00372UK | garbage disposal | | KIT01729JP | microwave oven   | +------------+------------------+ 6 rows in set (0.00 sec) mysql> mysql> drop table housewares; Query OK, 0 rows affected (0.00 sec)