如何从具有不同ID的重复数据中获取第一个ID


How to get the first ID from the repeated data with different ID's

在一个表中,我有重复的数据,我需要清理但不删除,它必须"重新组织",所以我已经设法清理了大部分数据,作为回报,我有一个基本数组如下:

array (
    [0] => array (
    [id] => 1
    [email] => tu@al.com
    )
    [1] => array (
    [id] => 2
    [email] => tu@al.com
    )
    [2] => array (
    [id] => 3
    [email] => ta@aa.com
    )
    [3] => array (
    [id] => 4
    [email] => tu@al.com
    ) // ... and so on about 10K records...
)
所以我有这段代码,所以我可以做一些深入的清理…
$rep = array();
foreach ($f as $cl=>$vl) {
    if(!in_array($vl['email'], $rep)) {
        $rep[] = $vl['email'];
        // Unique Data just leave it as this data is in "clean" table
    } else {
        // Repeated Data, insert it in a different Table with association ID
        // Delete this record with ID X from Clean Table
    }
}

事情是这样的…为了让我建立一个"好的"关联,我需要"移动"重复的数据,在这种情况下ID为2和4,所以我使用INSERT INTO…但是我需要具有重复数据的第一个记录的ID,这样我就可以将信息关联…

表1:

-------------- 
ID | email    |
1  |tu@al.com |
2  |tu@al.com |
3  |ta@aa.com | <-- Unique record
4  |tu@al.com |
---------------
// Code here to do the magic

表协会:

------------------------ 
ID | id_tb1 | Email     |
1  |1       | tu@al.com |
2  |1       | tu@al.com |
------------------------

当foreach完成后,最终的表应该是这样的,也就是" table 1"

-------------- 
ID | email    |
1  |tu@al.com |
3  |ta@aa.com |
---------------

制作表1花了我大约4个小时,因为这个"脏"表的列上有json,所以我必须清理所有这些并将信息关联起来,以便制作"表1",现在表1有大约21列,但最重要的是电子邮件…

因此,如果您只关心获得没有重复电子邮件的列表的第一个id,我建议使用这样的查询:

SELECT * FROM table_1 GROUP BY email ORDER BY id;

如果你想在另一个表格中获取数据只需要插入,你知道:

INSERT INTO table_2([columns]) (SELECT * FROM table_1 GROUP BY email ORDER BY id);

请记住两个表必须获得相同的列和顺序,或者在Insert和Select中指定列(按顺序)。

基于我所看到的,我必须构建另一个数组,在ELSE之后,我必须放置另一个foreach…

$rep = array();
$rep2 = array();
foreach ($f as $cl=>$vl) {
    if(!in_array($vl['email'], $rep)) {
        $rep[] = $vl['email'];
        $rep2[] = array('id'=>$vl['id'], 'emails'=>$vl['email']);
        // Unique Data just leave it as this data is in "clean" table
    } else {
        // Repeated Data, insert it in a different Table with association ID
        // Delete this record with ID X from Clean Table
        foreach (rep2 as $kr => $vr ) {
            if($vr['emails'] == $vl['email']) {
              // Here is where I get the ID of the first record that latter 
              // get repeated... 
              echo 'Repeated Record ID unique: ' . $vr['id]; // in this case is ID 1
              // Now we can insert the repeated data in the assoc Table with ID
              // Repetition, just like in table assoc... 
            }
        }

    }
}

问题解决了…:)

如果你想要所有的数据使用这个

$rep = array();
$first_id = array();
foreach ($f as $cl=>$vl) {
if(!in_array($vl['email'], $rep)) {
    $rep[] = $vl['email'];
    if(!in_array($vl['email'], $first_id))
        {
            $first_id[$vl['id']]=$vl['email'];
        }
    // Unique Data just leave it as this data is in "clean" table
} else {

    // Repeated Data, insert it in a different Table with association ID
    // Delete this record with ID X from Clean Table
}
}

如果你只想重复使用

$rep = array();
$first_id = array();
foreach ($f as $cl=>$vl) {
if(!in_array($vl['email'], $rep)) {
    $rep[] = $vl['email'];
    // Unique Data just leave it as this data is in "clean" table
} else {
    if(!in_array($vl['email'], $first_id))
        {
            $first_id[$vl['id']]=$vl['email'];
        }
    // Repeated Data, insert it in a different Table with association ID
    // Delete this record with ID X from Clean Table
}
}