由于之前的业务,造成数据库上产生了脏数据,写个脚本删除重复的数据。由于是开发测试环境,所以选择任意删除相同uid中的一条。由于每次执行只删除重复数据的一条,需要重复执行,如果本轮没有数据被删就OK
#!/bin/sh
# delete all company's duplicate uid
MYSQL_BIN_PATH=/data/mysql/server/mysql_3306/bin
MYSQL_SOCK_PATH=/data/mysql/server/mysql_3306/tmp
DBUSER=dbuser
DBPWD=userpwd
DBHOSTNAME=192.168.1.105
PORT=3306
# get all company_id
for company_id in `${MYSQL_BIN_PATH}/mysql -u${DBUSER} -p${DBPWD} -h ${DBHOSTNAME} -P ${PORT} --socket=${MYSQL_SOCK_PATH}/mysql.sock -e "
SELECT company_id FROM company.companypage;"`
do
if [ $company_id != "company_id" ] ; then
# if [ $company_id -eq 2733 ] ; then
suffix=`expr ${company_id} % 100`
for user_id in `${MYSQL_BIN_PATH}/mysql -u${DBUSER} -p${DBPWD} -h ${DBHOSTNAME} -P ${PORT} --socket=${MYSQL_SOCK_PATH}/mysql.sock -e "
SELECT user_id FROM company.company_candidate_${suffix} WHERE company_id=${company_id} AND user_id>0 GROUP BY company_id, user_id HAVING COUNT(user_id) > 1;"`
do
if [ $user_id != "user_id" ] ; then
${MYSQL_BIN_PATH}/mysql -u${DBUSER} -p${DBPWD} -h ${DBHOSTNAME} -P ${PORT} --socket=${MYSQL_SOCK_PATH}/mysql.sock -e "
DELETE FROM company.company_candidate_${suffix} WHERE company_id=${company_id} and user_id=${user_id} limit 1;"
echo "delete from company_candidate_${suffix} where company_id=${company_id} and user_id=${user_id} limit 1"
fi
done
# fi
fi
done
exit 0
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对软件开发网的支持。如果你想了解更多相关内容请查看下面相关链接
您可能感兴趣的文章:sql server通过脚本进行数据库压缩全备份的方法【推荐】sqlserver 使用SSMS运行sql脚本的六种方法使用shell脚本每天对MySQL多个数据库自动备份的讲解shell中循环调用hive sql 脚本的方法用shell写一个mysql数据备份脚本Navicat for MySQL导出表结构脚本的简单方法MySQL从命令行导入SQL脚本时出现中文乱码的解决方法sql脚本函数编写postgresql数据库实现解析