最近在负责一个项目的落地工做,须要天天导出客户通信录进行统计各地区注册用户数、使用用户数、未使用用户数、注册不符合规范的用户等等操做,刚开始用户数量比较少,直接在excel中筛选查询就行,可是随着用户数量的增长到几十万,excel筛选已没法知足需求,全部就想着导入到MySQL数据库中进行查询,这样就起到事倍功半的效果.mysql
1.首先用MySQL工具Navicat for MySQL导入excel表,excel表格编码格式为UTF-8格式.sql
我将excel表格导入MySQL db0库中,也须要设置编码为UTF-8格式;shell
mysql> show create database db0; +----------+--------------------------------------------------------------+ | Database | Create Database | +----------+--------------------------------------------------------------+ | db0 | CREATE DATABASE `db0` /*!40100 DEFAULT CHARACTER SET utf8 */ | +----------+--------------------------------------------------------------+ 1 row in set (0.00 sec)
2.若是第一步能将excel表成功导入数据库中,那就成功了通常,剩下的就是用sql对数据库的操做了,可是个人通信录里面有三十一个省份自治区直辖市的用户,若是一条条sql统计的话也会比较的麻烦,全部就考虑本身写脚原本操做数据库了。数据库
因为每次查询都须要登陆数据库,全部将数据库用户名密码都保存在文件中,这样就能够直接执行脚本,也不担忧执行提示明文密码不安全的警告.将用户数据库密码保存在/etc/my.cnf文件中,定义以下:安全
# cat /etc/my.cnf [client] host=localhost port=3306 user=root password=123456
而后再脚本中加入一下行,使用是$MySQL -e "SQL语句" 便可. bash
#sed -i 's/x190920/x190927/g' *.sh MySQL="mysql --defaults-extra-file=/etc/my.cnf" #$MySQL -e "use db0;select * from 数据库名称;"
3.接下来就是根据需求来统计用户数据,能够经过shell脚本实现.函数
脚本示例:工具
1.>统计各省的人数及总人数,其中memberlist.txt文件保存的是各省的名称,编码
#!/bin/bash ######################################## #注册总人数 ######################################## MySQL="mysql --defaults-extra-file=/etc/my.cnf" #统计各省的人数 function statistics() { for i in $(cat memberlist.txt |awk '{print $1}') do result=`$MySQL -e "use db0;select count(*) from x190927 where 部门 like '%$i%';"` echo $result >> tmp.txt echo -e "\033[1;3;32m$i的统计人数为:\033[0m" echo -e "\033[1;3;33m$result\033[0m" 2>/dev/null done } #统计总人数 function Summation(){ Accumulate=0 total=`cat tmp.txt|awk '{print $2}'` for n in $total do let Accumulate+=$n done echo -e "\033[1;3;34m注册总人数为:\n $Accumulate \033[0m" } statistics Summation rm -rf /root/script/tmp.txt
2.示例二:统计各省市注册电话为空的用户总数及总数spa
在这里统计时将各省市的统计结果保存到excel中.
#!/bin/bash ######################################## #统计各省市注册用户电话为空的用户 ######################################## MySQL="mysql --defaults-extra-file=/etc/my.cnf" /usr/bin/rm -rf /var/lib/mysql-files/* function statistics() { for i in $(cat memberlist.txt |awk '{print $1}') do result=`$MySQL -e "use db0;select 姓名,账号,手机,部门 from x190927 where length(手机) is null and 部门 like '%$i%' into outfile '/var/lib/mysql-files/$i.xls' character set gbk;"` #每一个部门手机号为空的用户 tel_numbe_null=`$MySQL -e "use db0;select count(*) from x190927 where length(手机) is null and 部门 like '%$i%';"` echo $tel_numbe_null >> tmp.txt echo -e "\033[1;3;32m$i的统计人数为:\033[0m" echo -e "\033[1;3;33m$tel_numbe_null\033[0m" 2>/dev/null done } function Summation(){ Accumulate=0 total=`cat tmp.txt|awk '{print $2}'` for n in $total do let Accumulate+=$n done echo -e "\033[1;3;34m无手机号的注册总人数为:\n $Accumulate \033[0m" } statistics Summation rm -rf /root/script/tmp.txt
这里面要注意几个知识点:
1.>导出的excel文件保存在 /var/lib/mysql-files/ 目录中,mysql安全方面的要求.
2. >sql 的 length函数,用来判断字段列的长度的,count函数用来求和的.
3.>注意设置excel的字符集,否则导出后打开会乱码 ,这里设置的是 character set gbk;
3.因为使用上面导出excel表没有列名,看起来不是很友好,示例三就讲解导出的表格中也带有列名
sql模板:(这样导出后就会有姓名、帐号、手机号、部门的列名称,主要熟悉写法和后面的字段含有.)
select * from ( select '姓名' as 姓名,'账号' as 账号,'手机号' as 手机号,'部门' as 部门 union all select 姓名,账号,手机,部门 from x190927 where length(手机) is null and 部门 like '%北京市%' ) a into outfile '/var/lib/mysql-files/888.xls' character set gbk fields terminated by '\t' OPTIONALLY ENCLOSED BY '"' lines terminated by '\n';
示例脚本:
#!/bin/bash ######################################## #统计各省市注册用户电话为空的用户 ######################################## MySQL="mysql --defaults-extra-file=/etc/my.cnf" /usr/bin/rm -rf /var/lib/mysql-files/* function statistics() { for i in $(cat memberlist.txt |awk '{print $1}') do result=`$MySQL -e "use db0; select * from (select '姓名' as 姓名,'账号' as 账号,'手机号' as 手机号,'部门' as 部门 union all select 姓名,账号,手机,部门 from x190927 where length(手机) is null and 部门 like '%$i%') a into outfile '/var/lib/mysql-files/$i.xls' character set gbk fields terminated by '\t' lines terminated by '\n';"` #每一个部门手机号为空的用户 tel_numbe_null=`$MySQL -e "use db0;select count(*) from x190927 where length(手机) is null and 部门 like '%$i%';"` echo $tel_numbe_null >> tmp.txt echo -e "\033[1;3;32m$i的统计人数为:\033[0m" echo -e "\033[1;3;33m$tel_numbe_null\033[0m" 2>/dev/null done } function Summation(){ Accumulate=0 total=`cat tmp.txt|awk '{print $2}'` for n in $total do let Accumulate+=$n done echo -e "\033[1;3;34m无手机号的注册总人数为:\n $Accumulate \033[0m" } statistics Summation rm -rf /root/script/tmp.txt
将excel文件导出到/var/lib/mysql-files目录中,好像须要在/etc/my.cnf中设置以下参数.
cat /etc/my.cnf [mysqld] validate_password=off #关闭密码安全策略 default_password_lifetime=0 #设置密码不过时
这就是本身在写脚本中掌握和遇到的,记录下以便于之后使用.
我在导入excel刚开始时,数据量在几万条导入数据库没问题,可是excel数据在10多万条时导入显示成功,但数据库里面就几千条数据,查缘由查了半天也没解决,最后只能将excel转换成txt格式的导入数据库,导入txt文档是注意编码格式.