gpt4 book ai didi

php - MySQL 排序英国邮政编码

转载 作者:行者123 更新时间:2023-11-29 07:01:37 25 4
gpt4 key购买 nike

我目前有一个简单的 MySQL 选择,在名为 postcodes 的列上使用 order by。这些邮政编码是英国的。

目前输出顺序为:SE1, SW1, SE10, SE11, SE2, SW2, SE3。我知道使用 ABS() 会更正数字顺序,但我不确定在这种情况下我会怎么做,因为有字母和数字。

我想按以下顺序显示它们:
SE1、SE2、SE3 SE10、SE11、SW1、SW2

感谢您的帮助。

最佳答案

您可以向表中添加一些额外的列以包含其中的片段,并通过使用第三列作为排序依据来返回顺序。例如:

PostCodeTable
|---------------------------------------------|
|postCode| codePart1 | codePart2 | codePart3 |
|char() | char() | int() | whatever() |
|---------------------------------------------|
| SE1 | SE | 1 | |
| SW1 | SW | 10 | |
| SE10 | SE | 10 | |
| SE11 | SE | 11 | |
| SE2 | SE | 2 | |
| SW2 | SW | 2 | |
| SE3 | SE | 3 | |
|---------------------------------------------|

您的查询可能是:

Select
postCode
from
PostCodeTable
order by
codePart1,
codePart2
// etc etc as needed.

关于php - MySQL 排序英国邮政编码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9913140/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com