MySQL: Add sequence column based on another field

前端 未结 3 894

I\'m working on some legacy code/database, and need to add a field to the database which will record a sequence number related to that (foreign) id.

Example table da

3条回答
  •  谎友^
    谎友^ (楼主)
    2020-11-27 18:10

    The question is tagged as "mysql", so yes, MySQL's auto_increment can create groupwise sequential ids.
    see http://dev.mysql.com/doc/refman/5.0/en/example-auto-increment.html:

    For MyISAM and BDB tables you can specify AUTO_INCREMENT on a secondary column in a multiple-column index. In this case, the generated value for the AUTO_INCREMENT column is calculated as MAX(auto_increment_column) + 1 WHERE prefix=given-prefix. This is useful when you want to put data into ordered groups.

    edit: example php script (using PDO, but it's the same game with the php-mysql module)

    $pdo = new PDO('mysql:host=...;dbname=...', '...', '...'); 
    $pdo->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
    
    // example table
    $pdo->exec(
      'CREATE TEMPORARY TABLE Foo (
        id int auto_increment,
        account int,
        someotherstuff varchar(32),
        primary key(account,id)
      ) engine=MyIsam'
    );
    // insert example data
    $stmt = $pdo->prepare('INSERT INTO Foo (account,someotherstuff) VALUES (?,?)');
    $stmt->execute(array(1, '1a'));
    $stmt->execute(array(1, '1b'));
    $stmt->execute(array(1, '1c'));
    $stmt->execute(array(2, '2a'));
    $stmt->execute(array(2, '2b'));
    $stmt->execute(array(1, '1d'));
    unset($stmt);
    
    // query data
    foreach( $pdo->query('SELECT account,id,someotherstuff FROM Foo') as $row ) {
      echo $row['account'], ' ', $row['id'], ' ', $row['someotherstuff'], "\n";
    }
    

    prints

    1 1 1a
    1 2 1b
    1 3 1c
    2 1 2a
    2 2 2b
    1 4 1d
    

提交回复
热议问题