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 />";}
}

?>

mysql查找重复字段并且删除保留一个:等您坐沙发呢!

发表评论

表情
还能输入210个字