如何正确排序MySQL表

时间:2014-01-07 17:06:41

标签: mysql sequencing

我有一张包含左右鞋混合物的桌子,其中一些是防水的。

我需要编写一个查询按字母顺序排序,但是当名称相同时 - 使用左/右前方的防水栏。

e.g。

+-------------------------+------------+------------+
| Shoe name               | Waterproof | Left/Right |
+-------------------------+------------+------------+
| boot                    |     0      |   left     |
| sandal                  |     0      |   left     |
| shoe                    |     1      |   left     |
| boot                    |     1      |   left     |
| boot                    |     0      |   right    |
| boot                    |     1      |   right    |
| sandal                  |     0      |   right    |
| shoe                    |     1      |   right    |
+-------------------------+------------+------------+

应按此分类......

+-------------------------+------------+------------+
| Shoe name               | Waterproof | Left/Right |
+-------------------------+------------+------------+
| boot                    |     0      |   left     |
| boot                    |     0      |   right    |
| boot                    |     1      |   left     |
| boot                    |     1      |   right    |
| sandal                  |     0      |   left     |
| sandal                  |     0      |   right    |
| shoe                    |     1      |   left     |
| shoe                    |     1      |   right    |
+-------------------------+------------+------------+

可以吗?

3 个答案:

答案 0 :(得分:1)

  

但如果凉鞋不防水怎么办?

在修改后的数据结构中,以下内容应该有效:

SELECT `Shoe name`, `Waterproof`, `Left/Right`
 FROM shoe_table
  ORDER BY CONCAT( `Shoe name`, ', ', `Waterproof` ), `Left/Right`;

您可以尝试:

select * from shoes order by 2, 1;

按列名称:

select * from shoes order by Waterproof, `Shoe name`;

答案 1 :(得分:0)

试试这个:

ORDER BY ShoeName,Waterproof 

答案 2 :(得分:0)

这需要解析shoes列。最简单的方法是使用substring_index()

order by substring_index(ShoeName, '(', 1), Waterproof, ShowName