【问题标题】:Find Duplicate records from a table one after another in codeigniter在codeigniter中依次从表中查找重复记录
【发布时间】:2019-02-18 02:26:11
【问题描述】:

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

 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] =&gt; dateformat 出现了 3 次,[name] =&gt; 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
        )

任何帮助将不胜感激。

【问题讨论】:

    标签: codeigniter phpmyadmin


    【解决方案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);
    }
    

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

    【讨论】:

    • @AlekStephanok 看来这两个数组都是一样的。您在更新前后提供的。请清除。
    • 您将在所需的输出中看到 [name] => dateformat 出现 3 次,然后 [name] => companyname 即将出现.....
    • 是的,我查过了。如果您将在名称列上添加顺序,那么相同的名称字段将聚集在一起。
    • @AlekStephanok 如果此答案对您有帮助,请对此进行投票并单击检查以进行验证。这将不胜感激。
    猜你喜欢
    • 2019-08-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-25
    • 2015-01-15
    • 1970-01-01
    相关资源
    最近更新 更多