在Codeigniter中一个接一个地查找表中的重复记录

时间:2018-09-13 10:55:31

标签: codeigniter phpmyadmin

我想在表中查找重复记录。

 public function duplicate_records() {
       $sql = " select * from tbloptions where name in (select name from tbloptions group by name having COUNT(*)>1)";
        $result = $this->db->query($sql);
        $row = $result->result();
        echo '<pre>';
        print_r($row);
    }

我编写了上面的查询,并得到以下输出:-

Array
(
    [0] => stdClass Object
        (
            [id] => 1
            [name] => dateformat
            [value] => d-m-Y|%d-%m-%Y
            [autoload] => 1
        )

    [1] => stdClass Object
        (
            [id] => 2
            [name] => companyname
            [value] => 01Crypto.com
            [autoload] => 1
        )

    [2] => stdClass Object
        (
            [id] => 316
            [name] => dateformat
            [value] => d-m-Y|%d-%m-%Y
            [autoload] => 1
        )

    [3] => stdClass Object
        (
            [id] => 317
            [name] => dateformat
            [value] => d-m-Y|%d-%m-%Y
            [autoload] => 1
        )

    [4] => stdClass Object
        (
            [id] => 318
            [name] => companyname
            [value] => 01Crypto.com
            [autoload] => 1
        )

)

因此,在输出[name] => dateformat中出现3次,而[name] => companyname中出现2次。

修改:-

我想要以下输出:-

[0] => stdClass Object
        (
            [id] => 1
            [name] => dateformat
            [value] => d-m-Y|%d-%m-%Y
            [autoload] => 1
        )
[2] => stdClass Object
        (
            [id] => 316
            [name] => dateformat
            [value] => d-m-Y|%d-%m-%Y
            [autoload] => 1
        )
[3] => stdClass Object
        (
            [id] => 317
            [name] => dateformat
            [value] => d-m-Y|%d-%m-%Y
            [autoload] => 1
        )
[1] => stdClass Object
        (
            [id] => 2
            [name] => companyname
            [value] => 01Crypto.com
            [autoload] => 1
        )
[4] => stdClass Object
        (
            [id] => 318
            [name] => companyname
            [value] => 01Crypto.com
            [autoload] => 1
        )

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

据我所知,您需要对此做出回应。

Array (
    [0] => stdClass Object
        (
            [id] => 2
            [name] => companyname
            [value] => 01Crypto.com
            [autoload] => 1
        )
    [1] => stdClass Object
        (
            [id] => 318
            [name] => companyname
            [value] => 01Crypto.com
            [autoload] => 1
        )
    [2] => stdClass Object
        (
            [id] => 1
            [name] => dateformat
            [value] => d-m-Y|%d-%m-%Y
            [autoload] => 1
        )
    [3] => stdClass Object
        (
            [id] => 316
            [name] => dateformat
            [value] => d-m-Y|%d-%m-%Y
            [autoload] => 1
        )
    [4] => stdClass Object
        (
            [id] => 317
            [name] => dateformat
            [value] => d-m-Y|%d-%m-%Y
            [autoload] => 1
        )
)

如果我是对的,那么您可以尝试以下给出的示例。那会帮助你的。

public function duplicate_records() {
   $sql = " select *, GROUP_CONCAT(id) as ids from tbloptions where name in (select name from tbloptions group by name having COUNT(*)>1) group by name order by name";
    $result = $this->db->query($sql);
    $row = $result->result();
    echo '<pre>';
    print_r($row);
}

或者,如果我错了,请分享您需要的输出。我将更新答案。