user3419304
user3419304

Reputation: 279

how to OrderBy Ascending of mixed string/int column in MySQL?

i m having the table like this

------------------------
Id  |  MergeId  | name |
------------------------
1   |   M1      | Riya |
2   |   M2      | diya |
3   |   M3      | tiya |
------------------------

Now i need to select table OrderBy Ascending MergeId (M1,M2,M3,M4.....)

Upvotes: 0

Views: 60

Answers (1)

juergen d
juergen d

Reputation: 204746

You need to remove the M and then cast the value to a number. You can use a mathematical operation for that to do it implicitly

select * from your_table
order by substring(mergeId, 2) * 1 asc

or do it explicitly with

order by cast(substring(mergeId, 2) as unsigned) asc

Upvotes: 1

Related Questions