Can we extract max value using regexp_substr in mysql?

Discussion in 'MySQL' started by asifakhtar, Dec 12, 2022.

  1. #1
    I get a dynamic string with values separated by comma. Need to extract max value using regexp_substr.

    e.g "55,222,3,4000,10" I need to extract 4000 in the above case using regex in MySQL.
     
    asifakhtar, Dec 12, 2022 IP
  2. qwikad.com

    qwikad.com Illustrious Member Affiliate Manager

    Messages:
    7,309
    Likes Received:
    1,702
    Best Answers:
    31
    Trophy Points:
    475
    #2
    I'd do this using an array. Something like:

    
    <?php
       $numbers = array(55,222,3,4000,10);
       foreach ($numbers as $i);
       echo max($numbers);
    ?>
    
    Code (markup):
    Example: https://onecompiler.com/php/3yrpmwhvp
     
    qwikad.com, Dec 12, 2022 IP
  3. sarahk

    sarahk iTamer Staff

    Messages:
    28,830
    Likes Received:
    4,541
    Best Answers:
    123
    Trophy Points:
    665
    #3
    So the numbers are stored in a single column in the database and you need to extract the greatest as part of your query?
    I'd create a stored procedure that will explode the string, sort in numerical order and return the max value. You then call the stored procedure from your query.
     
    sarahk, Dec 14, 2022 IP