我有数百个MySQL表,我想用每个MySQL表的第一列创建一个ascii表。
来自MySQL表
table A table B ... table Z
A1 A2 A3 B1 B2 B3 Z1 Z2 Z3
A1 A2 A3 B1 B2 B3 Z1 Z2 Z3
A1 A2 A3 B1 B2 B3 Z1 Z2 Z3
到ascii文件
A1 B1 ... Z1
A1 B1 ... Z1
A1 B1 ... Z1
哪种方法更快?
这些表有数百个,列有数千行,并且大多数列都有相同的行数(所以我不认为在出口之前是"加入"必要)
非常感谢
答案 0 :(得分:0)
我不是bash专家,但我一步一步做事(其他专家可以编辑和改进我的答案)
#!/bin/bash
mysql -uusrname -ppassword -Ddatabasename -s -e 'show tables'
count=0
while read tablenames
do
fieldname=$(mysql -uusrname -ppassword -Ddatabasename -s -e "desc $tablename" | head -2 | tail -1 | awk -F " " '{print $1}');
echo "$tablenames,$fieldname" >> tempfile
done< $(mysql -uusrname -ppassword -Ddatabasename -s -e 'show tables')
lastcount=$(wc -l tempfile)
while read line
do
count=$(($count + 1))
if [ $count -eq 1 ]
then
echo "nothing to do"
elif [ $count -lt $lastcount ]
then
echo $line | awk -F "," '{print "(select "$2 "from" $1")"}' >> sqlcommand.sql
echo "union" >> sqlcommand.sql
else
echo $line | awk -F "," '{print "(select "$2 "from" $1");"}' >> sqlcommand.sql
done < tempfile
mysql -uusrname -ppassword -Ddatabasename << sqlcommand.sql >> outputasciifile.txt