mysql converts a string to a number

created at 12-27-2021 views: 2

When I wrote the SQL statement today, I sorted the numbers relative to the string type. How to do it?

Need to be converted to numbers before sorting

1. Use + directly

String+0

eg:

select * from orders order by (mark+0)  desc

2. Use functions

  • CAST(value as type);

  • CONVERT(value, type);

Note:

The type here can be:

  • float: DECIMAL
  • Integer: SIGNED
  • unsigned integer: UNSIGNED

eg:

select * from orders order by CONVERT(mark,SIGNED)  desc

select * from orders order by CAST(mark as SIGNED)  desc
created at:12-27-2021
edited at: 12-27-2021: