PHP mysqli:如果发生唯一冲突,则插入行或返回id

时间:2015-12-19 14:38:39

标签: php mysqli

我有一张重量值表:

TABLE_WEIGHT
    WEIGHT_ID bigint(20) NOT NULL PRIMARY KEY AUTO_INCREMENT,
    WEIGHT float NOT NULL UNIQUE

我插入值:

$query = "INSERT INTO TABLE_WEIGHT (WEIGHT) VALUES(100)";

if ($stmt = $this->mysqli->prepare($query)) : 
    if (!stmt->execute()) : 
        // some error
    else :
        // alright
    endif;
endif;

我第一次这样做,我没有问题。如果我这样做,第二次值100已经存在。有没有办法在不需要新的select语句的情况下获取冲突行的id?

1 个答案:

答案 0 :(得分:1)

您可以通过INSERT ...ON DUPLICATE KEY UPDATE和鲜为人知的LAST_INSERT_ID(expr)

形式来实现这一目标
<?php
$pdo = new PDO('mysql:host=localhost;dbname=test;charset=utf8', 'localonly', 'localonly', array(
    PDO::ATTR_EMULATE_PREPARES=>false,
    PDO::MYSQL_ATTR_DIRECT_QUERY=>false,
    PDO::ATTR_ERRMODE=>PDO::ERRMODE_EXCEPTION
));
setup($pdo);

$stmt = $pdo->prepare('
    INSERT INTO
        soFoo
        (WEIGHT)
    VALUES
        (?)
    ON DUPLICATE KEY UPDATE
        WEIGHT_ID=LAST_INSERT_ID(WEIGHT_ID),
        dummy=NOT dummy
');

foreach( array(100,200,300,100,400) as $w ) {
    $stmt->execute( array($w) );
    echo $w, ' -> ', $pdo->lastInsertId(), "\r\n";
}


foreach( $pdo->query('SELECT WEIGHT_ID,WEIGHT FROM soFoo', PDO::FETCH_ASSOC) as $row) {
    echo join(', ', $row), "\r\n";
}


function setup($pdo) {
    $pdo->exec('
        CREATE TEMPORARY TABLE soFoo (
            WEIGHT_ID bigint(20) NOT NULL AUTO_INCREMENT,
            WEIGHT float NOT NULL UNIQUE,
            dummy   int  NOT NULL DEFAULT 0,
            PRIMARY KEY(WEIGHT_ID),
            UNIQUE KEY(WEIGHT)
        )
    ');
}

打印

100 -> 1
200 -> 2
300 -> 3
100 -> 1
400 -> 5
1, 100
2, 200
3, 300
5, 400

(注意缺少的WEIGHT_ID = 4)