mysql查找重复字段并且删除保留一个

<?php
define('IN_ECTOUCH', true);
define('IN_ECS', true);
require(dirname(dirname(__FILE__)) . '/includes/init.php');

//查找相同openid的数据并且判断openid不为空
$sql="select openid,count(*) as count from wechat_user where openid!='' group by openid having count>1 order by count desc limit 0,10;";
$arr=$db->getAll($sql);
foreach($arr as $key => $val){
	//查找最大uid
	$sql="select max(uid) from wechat_user where openid='".$val['openid']."'";
	$max_uid=$db->getOne($sql);
	//删除用户,并保留最大uid的用户
	$sql="delete from wechat_user where openid='".$val['openid']."' and uid!=".$max_uid;
	$d=$db->query($sql);

	if($d){echo "删除openid为:".$val['openid']."<br />";}
}

?>

未经允许不得转载:YiEcho博客 » mysql查找重复字段并且删除保留一个

赞 (0) 打赏

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏