简体   繁体   English

MySQL在表中插入多行(如果尚不存在)

[英]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. 我在PHP中有一个数组。 Let's call it $choices[] . 我们称之为$choices[] The column selection coresponds to an array index for the choices array. selection对应于choices数组的数组索引。 If I add an element or elements to the $choices array then I need to update the database. 如果我向$choices数组中添加一个或多个元素,则需要更新数据库。 I'd be operating in a loop where $document and $section are also set. 我将在其中还设置了$document$section的循环中操作。 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"? $choices每个元素检查(文档,节,选择),创建不存在的任何元素并将其状态设置为“新”的最有效方法是什么? 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. 如果所有内容都将使用“ new”进行更新,则可以轻松实现这一点。

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. 对于不存在的值,请尝试对其进行配置,以便在未指定时将其值默认为false。 Of course an INSERT statement can easily be adapted from my above UPDATE statement. 当然,可以根据我上面的UPDATE语句轻松地修改INSERT语句。

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. 无论它是否是最好的,我都不知道,但至少我没有向数据库发送数百个查询。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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