最近在負責一個項目的落地工作,需要每天導出客戶通訊錄進行統計各地區注冊用戶數、使用用戶數、未使用用戶數、注冊不符合規范的用戶等等操作,剛開始用戶數量比較少,直接在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 "\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.示例二:統計各省市注冊電話為空的用戶總數及總數
在這里統計時將各省市的統計結果保存到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文檔是注意編碼格式.
