Sorting mysql array value after string chars swapped from fetched DB data
I want to sort one column fetched from mysql DB, and stored in an array. After fetching I am doing below steps.
- DB Fetching fields array in row format. ->Field1, Field2, Field3, Field4, Field5
- From that fields array One columns data [Field3], swapping string keywords.
eg.
AB013, DB131, RS001
should become:013AB, 131DB, 001RS
Now I want to sort above value in new string format like-> 001RS, 013AB, 131DB
开发者_StackOverflow社区Its not like that, fix number for swapping string. To split and join string again there is a dynamic value which allow fetch data string to split that string from the given character position and then sort it again with new name.
Why don't you let the DB do the work (for the fixed position data in your example):
select Field1
, Field2
, concat( substr(Field3,3,3)
,substr(Field3,1,2)
) as SwapedField3
, Field4
, Field5
from Table
order by SwapedField3
However, this doesn't take the 'dynamic value' for the split position into account...
- Where does that value come from?
- Can it be deduced from the data somehow?
select * from (
select
Field1,
Field2,
CASE Field3
WHEN 'AB013' THEN '013AB'
WHEN 'DB131' THEN '131DB'
WHEN 'RS001' THEN '001RS'
END CASE AS NewField3,
Field4,
Field5
from Table1
)
order by NewField3
精彩评论