2

我试图在我的标题上对其进行总结,但让我在下面更详细地向您解释我正在尝试完成的工作。

目前我将一个数组传递给我的查询并使用一个 foreach 将每个数组插入到我的数据库中,如下所示。

if(is_array($myArr)){
 foreach($myArr as $userID=> $email){
  $sql = "INSERT INTO userdata (userID, email) values ('$userID', '$email')";
  mysql_query($sql) or exit(mysql_error());
 }
}

目前我的数组看起来如下

Array
(
[39] => sessa@test.com
[54] => sessa@test.com
[55] => sessa@test.com
[56] => sessa@test.com
[57] => mark@foo.com
[58] => mark@foo.com
[59] => mark@foo.com
[60] => mark@foo.com
[61] => sam@bar.com
[62] => sam@bar.com
[63] => sam@bar.com
[64] => sam@bar.com
)

'$userID' 是 [39] 和 'email' 是..well 电子邮件。

这就是我的问题所在。

我想要实现的是在我在这个“url”列中调用“url”的新列中插入一个新的 id,我希望能够增加 1 并将相同的值添加到具有相同电子邮件的行中。

所以像这样。

UserID |    email        | url  
===============================
  39   |  sessa@test.com |  1   
  54   | sessa@test.com  |  1  
  55   | sessa@test.com  |  1  
  56   | sessa@test.com  |  1  
  57   | mark@foo.com    |  2  
  58   | mark@foo.com    |  2  
  59   | mark@foo.com    |  2   
  60   | mark@foo.com    |  2  
  61   | sam@bar.com     |  3  
  62   | sam@bar.com     |  3  
  63   | sam@bar.com     |  3  
  64   | sam@bar.com     |  3  

我希望这是有道理的。感谢您的阅读。永远感谢您的帮助。

4

4 回答 4

4

我会选择 ON DUPLICATE KEY;

确保您在电子邮件上有一个唯一索引。

if(is_array($myArr)){
 foreach($myArr as $userID=> $email){
  $sql = "INSERT INTO userdata (userID, email) values ('$userID', '$email') 
        ON DUPLICATE KEY UPDATE url=url+1";
  mysql_query($sql) or exit(mysql_error());
 }
}

编辑:

好的,这可能是解决方案:

$data = array();
$ids = 1;

if(is_array($myArr)){
    foreach($myArr as $userID=> $email){

       $domain = substr(strstr($email,"@"),1);

        if (! isset($data[$domain]))
        {
            $data[$domain] = $ids;
            $ids ++;
        }

        $sql = "INSERT INTO userdata (userID, email,url) 
                VALUES ('$userID', '$email','".$data[$domain]."')";
        mysql_query($sql) or exit(mysql_error());
    }
}
于 2013-03-04T21:07:45.077 回答
1

您可以通过 1 个查询来组织它。

if(is_array($myArr)){
 foreach($myArr as $userID=> $email){
  $sql = "INSERT INTO `userdata` (`userID`, `email`, `url`) 
         select '$userID', '$email', count(`email`) +1 from `userdata` where `email` = '$email'";
  mysql_query($sql) or exit(mysql_error());
 }
}
于 2013-03-04T21:16:17.423 回答
1
 if(is_array($myArr)) : 

foreach($myArr as $userID=> $email){

    $emails[] = $email;
}

$emails = array_unique($emails);

foreach($emails as $e){
    $counter = 0;
    foreach($myArr as $userID=> $email){

        if($email == $e){

            $sql = "INSERT INTO userdata (userID, email,url) values ('$userID', '$email','$counter')";
            $counter++;
        }
    }

}

endif;

您可以使用它为所有插入创建一个 SQL 语句。

   if(is_array($myArr)) : 

    foreach($myArr as $userID=> $email){

        $emails[] = $email;
    }

    $emails = array_unique($emails);
    $sql = '';
            $counter = 1;
    foreach($emails as $e){

        foreach($myArr as $userID=> $email){

            if($email == $e){

                $sql .= "INSERT INTO userdata (userID, email,url) values ('$userID', '$email','$counter');";

            }
        }
        $counter++;
    }

    endif;

    mysql_query($sql) or exit(mysql_error());

SQL 输出

        INSERT INTO userdata (userID, email,url) values ('39', 'sessa@test.com','1');INSERT INTO userdata (userID, email,url) values ('54', 'sessa@test.com','1');INSERT INTO userdata (userID, email,url) values ('55', 'sessa@test.com','1');INSERT INTO userdata (userID, email,url) values ('56', 'sessa@test.com','1');INSERT INTO userdata (userID, email,url) values ('57', 'mark@foo.com','2');INSERT INTO userdata (userID, email,url) values ('58', 'mark@foo.com','2');INSERT INTO userdata (userID, email,url) values ('59', 'mark@foo.com','2');INSERT INTO userdata (userID, email,url) values ('60', 'mark@foo.com','2');INSERT INTO userdata (userID, email,url) values ('61', 'sam@bar.com','3');INSERT INTO userdata (userID, email,url) values ('62', 'sam@bar.com','3');INSERT INTO userdata (userID, email,url) values ('63', 'sam@bar.com','3');INSERT INTO userdata (userID, email,url) values ('64', 'sam@bar.com','3');
于 2013-03-04T21:06:20.470 回答
0

如果您有足够的内存 - 您可以再创建一个数组数据结构并存储电子邮件和 ID。

例如,您可以在 for 循环中执行类似的操作...

$data = array();
$i = 0;

if(is_array($myArr)){
    foreach($myArr as $userID=> $email){
        $url_id = '';

        if (! isset($data[$email]))
        {
            $data[$email] = ++$i;
        }

        $url_id  = $data[$email];

        $sql = "INSERT INTO userdata (userID, email,url) values ('$userID', '$email','$url_id')";
        mysql_query($sql) or exit(mysql_error());
    }
}

我不认为这是最有效的解决方案..但它应该工作..

于 2013-03-04T21:10:09.693 回答