MySQL get all affected rows for multiple statements in one query

后端 未结 3 763
太阳男子
太阳男子 2020-12-20 02:12

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 03:02

    I'd be doing it like this, but, I do like to keep things simple which not everyone can appreciate ;)

    $deleteContactSQL = sprintf("call cascade_delete_persons(%d,%d)", $person, $user);
    $deleteContacts = mysqli_query($link, $deleteContactSQL);
    
    drop procedure if exists cascade_delete_persons;
    
    delimiter #
    
    create procedure cascade_delete_persons
    (
    in p_pid int unsigned,
    in p_oid int unsigned
    )
    begin
    
    declare v_persons_count int unsigned default 0;
    declare v_tabs_count int unsigned default 0;
    
        delete from persons where id = p_pid and owner = p_oid and userOrContact = 'contact';
        set v_persons_count = row_count();
    
        delete from tabs where person = p_pid and ownerIdentity = p_oid and selfOrOther = 'other';
        set v_tabs_count = row_count();
    
        -- etc...
    
        select v_persons_count as person_count, v_tabs_count as tabs_count;
    
    end #
    
    delimiter ;
    

    You can use this method too if you must : http://php.net/manual/en/mysqli.multi-query.php

提交回复
热议问题