Search code examples
mysqlsqlmaxvarchar

MySQL get MAX of a varchar like "500x400"


Is there any chance to get in MySQL the MAX of a column containing varchar like "500x400" or "800x600". They are sizes of frames.

I would need the max of the multiplication of the 2 numbers. 500x400 => 200 000.

The my_frame_size table is like

+--------------------+
  Id      frame_size
+--------------------+
  1        500x400
  2        800x600
  3        400x300
  4        600x400
  5        1000x700

Solution

  • You can easily take take the max . . . but it is not what you want. Instead, calculate the area:

    select t.*,
           substring_index(frame_size, 'x', 1) * substring_index(frame_size, 'x', -1) as area
    from t;
    

    You can order by the area or use max() on the calculation to get the maximum size.