Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php MySQL按一列排序,该列有多个用斜杠分隔的数值_Php_Mysql - Fatal编程技术网

Php MySQL按一列排序,该列有多个用斜杠分隔的数值

Php MySQL按一列排序,该列有多个用斜杠分隔的数值,php,mysql,Php,Mysql,我有一个表(policy_details),其中有一列“notenumber”,其中包含以下值: 150/1 150/2 150/1/2 150/2/1 我需要按升序获取notenumber。最好的解决方案是重新设计模式,将notenumber的每个部分存储在不同的字段中。除此之外,您需要拆分notenumber的每个部分,将其转换为数字类型,并按其排序。您必须为拆分出的每个零件在订单中添加一个子句,这是无法避免的 例如: select notenumber, replace

我有一个表(policy_details),其中有一列“notenumber”,其中包含以下值:

150/1 150/2 150/1/2 150/2/1
我需要按升序获取notenumber

最好的解决方案是重新设计模式,将
notenumber
的每个部分存储在不同的字段中。除此之外,您需要拆分
notenumber
的每个部分,将其转换为数字类型,并按其排序。您必须为拆分出的每个零件在订单中添加一个子句,这是无法避免的

例如:

select
  notenumber,
  replace(substring(substring_index(notenumber, '/', 1), length(substring_index(notenumber, '/', 1 - 1)) + 1), '/', ''),
  replace(substring(substring_index(notenumber, '/', 2), length(substring_index(notenumber, '/', 2 - 1)) + 1), '/', ''),
  replace(substring(substring_index(notenumber, '/', 3), length(substring_index(notenumber, '/', 3 - 1)) + 1), '/', '')
from test
order by
  convert(replace(substring(substring_index(notenumber, '/', 1), length(substring_index(notenumber, '/', 1 - 1)) + 1), '/', ''), signed integer),
  convert(replace(substring(substring_index(notenumber, '/', 2), length(substring_index(notenumber, '/', 2 - 1)) + 1), '/', ''), signed integer),
  convert(replace(substring(substring_index(notenumber, '/', 3), length(substring_index(notenumber, '/', 3 - 1)) + 1), '/', ''), signed integer)
;
注意:您需要在每个
replace
子句中插入拆分部分的索引号两次


上述解决方案的SQL FIDLE:

请详细说明并包含您已经尝试过的代码,以及更多详细信息,好吗?您试图实现什么,当您执行代码时,当前正在发生什么?我已经尝试从策略中选择*,替换(notenumber,N'/',N'')为newnotenumber,按newnotenumber asc的顺序排列”;它是否没有给您预期的结果?是的,但按asc的顺序排列
select *,REPLACE(notenumber, N'/', N'') AS newnotenumber 
from policy_details 
order by newnotenumber asc;
select
  notenumber,
  replace(substring(substring_index(notenumber, '/', 1), length(substring_index(notenumber, '/', 1 - 1)) + 1), '/', ''),
  replace(substring(substring_index(notenumber, '/', 2), length(substring_index(notenumber, '/', 2 - 1)) + 1), '/', ''),
  replace(substring(substring_index(notenumber, '/', 3), length(substring_index(notenumber, '/', 3 - 1)) + 1), '/', '')
from test
order by
  convert(replace(substring(substring_index(notenumber, '/', 1), length(substring_index(notenumber, '/', 1 - 1)) + 1), '/', ''), signed integer),
  convert(replace(substring(substring_index(notenumber, '/', 2), length(substring_index(notenumber, '/', 2 - 1)) + 1), '/', ''), signed integer),
  convert(replace(substring(substring_index(notenumber, '/', 3), length(substring_index(notenumber, '/', 3 - 1)) + 1), '/', ''), signed integer)
;