MySQL get all affected rows for multiple statements in one query

后端 未结 3 1079
温柔的废话
温柔的废话 2020-12-20 02:08

MySQL/PHP:

For a query with multiple statements, which deletes rows in four different tables, I want to know the combined number of affected rows. T

3条回答
  •  时光取名叫无心
    2020-12-20 02:46

    Here is a compact, procedural-style mysqli_multi_query() solution for counting combined affected rows:

    $deleteContactSQL="DELETE FROM `persons` WHERE `id`='$person' AND `owner='$user' AND `userOrContact`='contact';
             DELETE FROM `tabs` WHERE `person`='$person' AND `ownerIdentity`='$user' AND `selfOrOther`='other';
             DELETE FROM `tabAccess` WHERE `person`='$person' AND `givenToIdentity`='$user';
             DELETE FROM `personAccess` WHERE `viewedPerson`='$person' AND `viewerIdentity`='$user';";
    include $_SERVER['DOCUMENT_ROOT'].'/goalview/includes/db.inc.php';
    if(mysqli_multi_query($link,$deleteContactSQL)){
        do{
            $success+=mysqli_affected_rows($link);
        }while(mysqli_more_results($link) && mysqli_next_result($link));
    }
    

    Alternatively, this group of queries may be a good candidate for some TRIGGERs in the persons table.

提交回复
热议问题