MySQL 中操作excel表格总结

最近在负责一个项目的落地工作,需要每天导出客户通讯录进行统计各地区注册用户数、使用用户数、未使用用户数、注册不符合规范的用户等等操作,刚开始用户数量比较少,直接在excel中筛选查询就行,但是随着用户数量的增加到几十万,excel筛选已无法满足需求,所有就想着导入到MySQL数据库中进行查询,这样就起到事倍功半的效果.

1.首先用MySQL工具Navicat for MySQL导入excel表,excel表格编码格式为UTF-8格式.

我将excel表格导入MySQL db0库中,也需要设置编码为UTF-8格式;

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语句" 即可. 

#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 "33[1;3;32m$i的统计人数为:33[0m"
    echo -e "33[1;3;33m$result33[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 "33[1;3;34m注册总人数为:
 $Accumulate 33[0m"
}

statistics
Summation
rm -rf /root/script/tmp.txt

2.示例二:统计各省市注册电话为空的用户总数及总数

在这里统计时将各省市的统计结果保存到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 "33[1;3;32m$i的统计人数为:33[0m"
    echo -e "33[1;3;33m$tel_numbe_null33[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 "33[1;3;34m无手机号的注册总人数为:
 $Accumulate 33[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 '	'
  OPTIONALLY ENCLOSED BY '"' 
  lines terminated by '
';

示例脚本:

#!/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 '	' lines terminated by '
';"`

#每个部门手机号为空的用户
    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 "33[1;3;32m$i的统计人数为:33[0m"
    echo -e "33[1;3;33m$tel_numbe_null33[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 "33[1;3;34m无手机号的注册总人数为:
 $Accumulate 33[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文档是注意编码格式.

原文地址:https://www.cnblogs.com/saneri/p/11600908.html