简体   繁体   中英

MySQL insert multiple rows into a table if they don't already exist

If I have a table like this:

+-------------+-------------+
| Field       | Type        |
+-------------+-------------+
| document    | varchar(35) |
| section     | int(11)     |
| selection   | int(11)     |
| status      | varchar(35) |
+-------------+-------------+

And I have an array in PHP. Let's call it $choices[] . The column selection coresponds to an array index for the choices array. If I add an element or elements to the $choices array then I need to update the database. I'd be operating in a loop where $document and $section are also set. I don't want to assume that there are no missing rows in the middle of my numbering. What's the most efficient way to check for (document,section,selection) for each element in $choices , create any that does not exist, and set its status to "new"? Do I have to do this as a loop, or is there a way to do this in one query?

This is completely untested, but this is basically how I'd do it if I didn't ask for help. I guessed someone might have a MUCH better solution than me.

foreach($documents as $document)
  {
    foreach($sections as $section)
      {
        $choices=$master_array[$document][$section];
        $ch_count=count($choices);
        $counter=0;
        while($counter < $ch_count-1)
          {
            $query="SELECT status FROM mytable WHERE document='$document' AND section='$section' AND selection='$counter'";
            $result = mysql_query($query);
            if(mysql_num_rows($result)==0)
              {
                $query="INSERT INTO mytable VALUES('$document','$section','$counter','new')";
                $result = mysql_query($query);
              }
            $counter++;
          }
      }
   } 

If everything is going to be updated with "new", then this can be achieved somewhat easily.

Examine the following code:

<?
$setStr = "";
foreach($choices as $column)
{
    $setStr .= $column . "=" . "'new', ";
}

$sqlCommand = "UPDATE table SET ".substr($setStr,'',-2)." WHERE pick_a_row_here;";
echo $sqlCommand;
?>

For the ones that do not exist, try configuring it so their values are default to false when unspecified. Of course an INSERT statement can easily be adapted from my above UPDATE statement.

Let me know what you think. If you have questions or comments, I'll do my best to answer them.

I ended up using a loop to contatenate together a large query string that looked something like this, except much larger:

INSERT IGNORE INTO mytable VALUES 
('doc1','1','0','New'),
('doc1','1','1','New'),
('doc1','1','2','New'),
('doc1','1','3','New'),
('doc1','2','0','New'),
('doc1','2','1','New'),
('doc1','2','2','New'),
('doc1','2','3','New'),
('doc1','3','0','New'),
('doc1','3','1','New'),
('doc1','3','2','New')

And it works. Whether or not its the best, I don't know but at least I'm not sending hundreds of queries to the DB.

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