我想在表中查找重复记录。
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
)
任何帮助将不胜感激。
据我了解,您需要对此做出回应。
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);
}
或者如果我错了,请分享您需要的输出。我会更新我的答案。