0

我有数百个 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

哪种方法更快?

表格有数百,列有数千行,并且大多数列的行数相同(所以我认为在导出之前没有必要“加入”)

非常感谢

4

1 回答 1

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
于 2013-11-05T07:56:00.887 回答