简体   繁体   中英

Deleting a row with inner join

I've created two tables, simplecomments and commentors , and connected them with INNER JOIN .

  • Simplecomments is details of each and every commenter, involving their comment, reg_date, commentorid etc...
  • Commentors is the personal info of a commenter with following columns: id, name, email..

I've joined them successfully, however I'm finding it hard to delete from the joined table.

I want to make it like this logic:

  1. If there's last row of a commentor called --let's say A-- then delete both his/her comment details and A himself/herself from the table.

  2. Else if A has commented plenty of times, with different comments, delete his/her comment details, but let his/her personal info remain since A has other comments there.

This is how I've made it:

if (!empty($_POST["delete"]))
{
    foreach ($_POST["delete"] as $key => $value) 
    {
        $resultid = $conn->query("SELECT commentorid FROM `simplecomments` WHERE id=".$value);
        $rowid = $resultid->fetch_assoc();

        $outputdelete = $rowid["name"] . " has been deleted" . "<br>";

        $deletedname = $deletedname.$outputdelete;
        $RES = mysql_num_rows($resultid);
        $counter = 0;
            while($row = $RES)
            {
               //IF IT'S LAST ROW, DELETE COMMENTOR AND HIS/HER COMMENTDETAILS 
                if(++$counter == $results) {
                    $resultid = $conn->query("DELETE FROM `commentor`");
                }
                //ELSE JUST DELETE HIS/HER COMMENTDETAILS, LET HIS/HER INFO REMAIN
                else{
                    $resultid = $conn->query("DELETE FROM `simplecomments` WHERE id=".$value);
                } 
            }
    }

}

However code won't work. I get an error:

Warning: mysql_num_rows() expects parameter 1 to be resource [..]...

Consider running DELETE...INNER JOIN and DELETE with subquery conditionals and avoid PHP query fetch looping with if/else as the logic seems to be the following:

  1. delete any commentor's profile and comments if he/she has only one comment
  2. delete only commentor's comments if he/she has multiple (ie, more than one) comments.

And yes, all three DELETE can be run at same time across all ids since mutually exclusive conditions are placed between the first two and last one. Therefore, either first two affects rows or last one affects rows per iteration. The unaffected one(s) will delete zero rows from either table.

Also, simplecomments records are deleted first since this table may have a foreign key constraint with commentor due to its one-to-many relationship. Finally, below assumes comment ids are passed into loop (not commentor id).

PHP (using parameterization, assuming $conn is a mysqli connection object)

foreach ($_POST["delete"] as $key => $value) {

   // DELETE COMMENTS AND THEN PROFILE FOR COMMENTORS WITH ONE POST    
   $sql = "DELETE FROM `simplecomments` s 
           WHERE s.id = ?
             AND (SELECT COUNT(*) FROM `simplecomments` sub
                  WHERE sub.commentorid = s.commentorid) = 1";
   $stmt = $conn->prepare($sql);
   $stmt->bind_param("i", $value);
   $stmt->execute();
   $stmt->close();

   $sql = "DELETE c.* FROM `simplecomments` c 
           INNER JOIN `simplecomments` s ON s.commentorid = c.id
           WHERE s.id = ?
             AND (SELECT COUNT(*) FROM `simplecomments` sub
                  WHERE sub.commentorid = s.commentorid) = 1";
   $stmt = $conn->prepare($sql);
   $stmt->bind_param("i", $value);
   $stmt->execute();
   $stmt->close();


   // DELETE COMMENTS FOR COMMENTORS WITH MULTIPLE POSTS BUT KEEP PROFILE
   $sql = "DELETE FROM `simplecomments` s
           WHERE s.id = ?
             AND (SELECT COUNT(*) FROM `simplecomments` sub
                  WHERE sub.commentorid = s.commentorid) > 1";    
   $stmt = $conn->prepare($sql);
   $stmt->bind_param("i", $value);
   $stmt->execute();
   $stmt->close();
}

Alternatively, for a DRY-er approach, loop SQL statements in an array:

$sqls = array(
           0 => "DELETE FROM `simplecomments` s WHERE s.id = ? AND (SELECT COUNT(*) FROM `simplecomments` sub WHERE sub.commentorid = s.commentorid) = 1",
           1 => "DELETE c.* FROM `simplecomments` c INNER JOIN `simplecomments` s ON s.commentorid = c.id WHERE s.id = ? AND (SELECT COUNT(*) FROM `simplecomments` sub WHERE sub.commentorid = s.commentorid) = 1",
           2 => "DELETE FROM `simplecomments` s WHERE s.id = ? AND (SELECT COUNT(*) FROM `simplecomments` sub WHERE sub.commentorid = s.commentorid) > 1"
        );

foreach ($_POST["delete"] as $key => $value) {
   foreach($sqls as $sql) {
       $stmt = $conn->prepare($sql);
       $stmt->bind_param("i", $value);
       $stmt->execute();
       $stmt->close();
   }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM