-1

我有一张桌子,里面放着左右鞋的混合物,其中一些是防水的。

我需要编写一个查询以按字母顺序对它们进行排序,但是 - 当名称相同时 - 使用左/右前面的防水列。

例如

+-------------------------+------------+------------+
| 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    |
+-------------------------+------------+------------+

可以做到吗?

4

3 回答 3

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`;
于 2014-01-07T17:08:14.617 回答
0

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

order by substring_index(ShoeName, '(', 1), Waterproof, ShowName
于 2014-01-07T17:22:56.153 回答
0

尝试这个:

ORDER BY ShoeName,Waterproof 
于 2014-01-07T17:08:26.553 回答