unique values in mysql field



  • How do you put unique values in mysql field? If yes:
    privet
    privet
    privet
    Other

    We need to write down.
    privet
    privet-1
    privet-2
    ♪ ♪ ♪ ♪

    Can you tell me how to make it more effective?
    What are the options?

    Alternatively, if the table field before the box is empty. (maybe it's working in a mass and then put it in the base once?)

    SELECT and check every time.
    But there's gonna be a lot of search operations... especially the big data, and it's gonna affect productivity.



  • This is an example of a function that can help:

    function getUniqueValue($value)
    {
        $value = trim($value);
    
    $i = 1;
    do {
        if (preg_match("/\-\d+$/i", $value)) {
            $value = preg_replace("/\-\d+$/i", sprintf("-%02d", $i++), $value, 1);
        } else {
            $value .= '-01';
        }
        $match = ... ;// здесь возвращается результат запроса с проверкой на наличие в базе такого поля
    } while ($match);
    
    return $value;
    

    }




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2