数据库查询重复行shell脚本
2014-01-09有时候表里会存在重复行。
1、oracle查询:
cat get_repeat_id.sh
#!/bin/bash
echo -e "select user_name from customer group by user_name having count(user_name) > 1;"|sqlplus -s 'user01/p123456'
根据查询得到的结果删除:
delete from customer a where a.user_name in "结果" and a.rowid not in (select min(b.rowid) from customer b group by b.user_name having count(user_name) > 1)
2、mysql实例:
#!/bin/bash
file=$1
if [ -z "$1" ];then
echo "Usage: $0 file"
exit 1;
fi
for id in `cat ${file}`;do
mysql -uroot -p123456 -Dmd5 -e "insert into md5 (str,value) values ('${id}',md5('${id}'));"
done
mysql -uroot -p123456 -Dmd5 -e 'select b.id from md5 b group by b.value having count(b.id) > 1' | tail -n +2 > repeat.txt
#将重复的id剔除
for id1 in `cat repeat.txt`;do
mysql -uroot -p123456 -Dmd5 -e "delete from md5 where id='${id1}'"
done
#将重复的id重新insert一次
for id2 in `cat repeat.txt`;do
mysql -uroot -p123456 -Dmd5 -e "insert into md5 (str,value) values ('${id2}',md5('${id2}'));"
done
相关日志
- oracle中drop一个表,让数据文件释放空间
- oracle批量杀掉死锁进程
- ORA-00257: archiver error. Connect internal only, until freed. (DBD ERROR: OCISessionBegin)
- oracle同义词SYNONYM
- substr()函数
- oracle create dblink
- oracle 启动归档模式
- oracle表空间文件
- linux删除oracle表空间文件后空间不释放
- nagios check_oracle_health install shell
- oracle最大连接数processes
- nagios check_oracle_health
- cx_Oracle for python
- oracle导入txt数据文件实例
- mysql导入txt数据文件实例
- 修改NLS_CHARACTERSET和NLS_NCHAR_CHARACTERSET字符集
- NLS_NCHAR_CHARACTERSET和NLS_CHARACTERSET的区别
- 如何安装mysql-5.6.12
- 设置NLS_LANG环境变量
- Oracle Golden Gate数据库同步技术
- mysql或oracle合并行函数
- Oracle InstantClient安装步骤
- shell sqlplus run sql
- oracle误删datafile
- mysqld_safe — MySQL Server Startup Script
- 如何禁用mysql端口3306
- oracle触发器实例 (8)
- sql语句处理BLOB (8)
- Drizzle A database for the Cloud (1)
- SGA/PGA的设置与调整 (9)
- 根据v$SGA_TARGET_ADVICE设置SGA SIZE (6)
- shmmax/shmall和sga_max_size/sga_target应该设置多大 (3)
- exp/imp和expdp/impdp的逻辑哲学 (1)
- oracle安装后期stop big port (1)
- OLTP和OLAP的区别 (3)
- mysql创建数据库及用户 (1)
- Mysql分支MariaDB简介 (6)
- ORA-01843: not a valid month (3)
- sql语句where 1=1和1=2的作用 (4)
- 如何安装mysql-5.5.31 (8)
- rlwrap实现sqlplus使用上下键查历史命令
- CentOS_x64_6.3 install Oracle 10.2.0.1报错ins_emdb.mk (2)