大概的需求是想把下面的warehouse字段拆分开,然后先按字母排序,再按第二个数字排序,在按第三个数字排序,以下代码仅作为参考。
SELECT id,warehouse,
#LENGTH( warehouse) - LENGTH(REPLACE( warehouse,'-','' )) AS 减号个数,
SUBSTRING_INDEX(warehouse,'-',1) AS 尺寸,
(
CASE
WHEN LENGTH( warehouse) - LENGTH(REPLACE( warehouse,'-','' ))=1 THEN substring(warehouse,LOCATE('-',warehouse)+1,length(warehouse)-1)+0
WHEN LENGTH( warehouse) - LENGTH(REPLACE( warehouse,'-','' ))=2 THEN SUBSTRING_INDEX(substring(warehouse,LOCATE('-',warehouse)+1,length(warehouse)-1),'-',1)+0
END
)
AS 楼层,
#LOCATE('-',substring(warehouse,LOCATE('-',warehouse)+1,length(warehouse)-1)) AS 第二个减号位置,
(
CASE
WHEN LENGTH( warehouse) - LENGTH(REPLACE( warehouse,'-','' ))=1 THEN 0
WHEN LENGTH( warehouse) - LENGTH(REPLACE( warehouse,'-','' ))=2 THEN substring(substring(warehouse,LOCATE('-',warehouse)+1,length(warehouse)-1),LOCATE('-',substring(warehouse,LOCATE('-',warehouse)+1,length(warehouse)-1))+1,length(substring(warehouse,LOCATE('-',warehouse)+1,length(warehouse)-1))-1)+0
END
)
AS 房号
FROM warehouse
ORDER BY 尺寸,楼层,房号
上面是mysql的,如果是sqlserver,需要简单的修改几个函数。
SELECT id,warehouse,
LEFT (warehouse, charindex('-', warehouse) - 1) AS 尺寸,
(
CASE
WHEN LEN( warehouse) - LEN(REPLACE( warehouse,'-','' ))=1 THEN substring(warehouse,charindex('-',warehouse)+1,LEN(warehouse)-1)+0
WHEN LEN( warehouse) - LEN(REPLACE( warehouse,'-','' ))=2 THEN LEFT(stuff(warehouse,1,charindex('-',warehouse),''),charindex('-',stuff(warehouse,1,charindex('-',warehouse),''))-1)+0
END
)
AS 楼层,
(
CASE
WHEN LEN( warehouse) - LEN(REPLACE( warehouse,'-','' ))=1 THEN 0
WHEN LEN( warehouse) - LEN(REPLACE( warehouse,'-','' ))=2 THEN substring(substring(warehouse,charindex('-',warehouse)+1,LEN(warehouse)-1),charindex('-',substring(warehouse,charindex('-',warehouse)+1,LEN(warehouse)-1))+1,LEN(substring(warehouse,charindex('-',warehouse)+1,LEN(warehouse)-1))-1)+0
END
)
AS 房号
FROM warehouse
ORDER BY 尺寸,楼层,房号
如果拆分出来的第二个字符串(也就是‘楼层’字段)中存在字母,可以再做修改,这里用正则,也可以用LIKE关键字做判断。
SELECT id,warehouse,
#LENGTH( warehouse) - LENGTH(REPLACE( warehouse,'-','' )) AS 减号个数,
SUBSTRING_INDEX(warehouse,'-',1) AS 尺寸,
(
CASE
WHEN LENGTH( warehouse) - LENGTH(REPLACE( warehouse,'-','' ))=1
THEN(
CASE
WHEN substring(warehouse,LOCATE('-',warehouse)+1,length(warehouse)-1) REGEXP '[a-z]'=0
THEN substring(warehouse,LOCATE('-',warehouse)+1,length(warehouse)-1)+0
WHEN substring(warehouse,LOCATE('-',warehouse)+1,length(warehouse)-1) REGEXP '[a-z]'=1
THEN substring(warehouse,LOCATE('-',warehouse)+1,length(warehouse)-1)
END
)
WHEN LENGTH( warehouse) - LENGTH(REPLACE( warehouse,'-','' ))=2
THEN(
CASE
WHEN SUBSTRING_INDEX(substring(warehouse,LOCATE('-',warehouse)+1,length(warehouse)-1),'-',1) REGEXP '[a-z]'=0
THEN SUBSTRING_INDEX(substring(warehouse,LOCATE('-',warehouse)+1,length(warehouse)-1),'-',1)+0
WHEN SUBSTRING_INDEX(substring(warehouse,LOCATE('-',warehouse)+1,length(warehouse)-1),'-',1) REGEXP '[a-z]'=1
THEN SUBSTRING_INDEX(substring(warehouse,LOCATE('-',warehouse)+1,length(warehouse)-1),'-',1)
END
)
END
)
AS 楼层,
#LOCATE('-',substring(warehouse,LOCATE('-',warehouse)+1,length(warehouse)-1)) AS 第二个减号位置,
(
CASE
WHEN LENGTH( warehouse) - LENGTH(REPLACE( warehouse,'-','' ))=1 THEN 0
WHEN LENGTH( warehouse) - LENGTH(REPLACE( warehouse,'-','' ))=2 THEN substring(substring(warehouse,LOCATE('-',warehouse)+1,length(warehouse)-1),LOCATE('-',substring(warehouse,LOCATE('-',warehouse)+1,length(warehouse)-1))+1,length(substring(warehouse,LOCATE('-',warehouse)+1,length(warehouse)-1))-1)+0
END
)
AS 房号
FROM warehouse
ORDER BY 尺寸,楼层,房号