MySQL order by string sort, MySQL string order by
==============================
? Copyright Sweet Potato Yiu September 30, 2017
http://fanshuyao.iteye.com/
First, the problem description:
MySQL sort if the field used is string type, the collation is like this: 1,10,2,20,3,4,5, this sort is based on the character from the first character to compare, but not what I want, I want is: 1,2,3,4,5......,10,20 this.
Second, the solution
When sorting, convert the corresponding field to integer, using the cast function, as follows:
SQL code
- CAST (Seat_row as UNSIGNED)
Complete Example:
SQL code
- SELECT * from ts_seat
- WHERE hall_id =840
- ORDER by cast (Seat_row as UNSIGNED),cast (Seat_col as UNSIGNED)
- ;
==============================
? Copyright Sweet Potato Yiu September 30, 2017
http://fanshuyao.iteye.com/
MySQL order by string sort, MySQL string order by