Mysql 多个相同结构的表查询某个字段

一、shell 命令行
for i in `mysql -uroot -pwindows@123 -e "show tables from dfsc"| grep 'call_records_'`; do mysql -uroot -pwindows@123 -e "select called_phone from dfsc.$i"|grep -v 'called_phone'; done

二、shell 脚本



#!/bin/bash
#multi-table query
#2019-03-29
#Mysql Version 5.7.25
echo >/tmp/select.txt #准备一个空文件存放结果
#从数据库中查询到需要的表,赋值给数组
ary=(`mysql -uroot -pwindows@123 -e "show tables from dfsc;"|grep 'call_records_'`)
#得到数组的长度,减1 赋值给一个变量,因为数组的下标是从0开始,所以要减1
a=`expr ${#ary[@]} - 1`
#for 循环 查询表,并过滤掉查询的字段,写入到文件。
for i in `seq 0 $a`;
do
mysql -uroot -pwindows@123 -e "select called_phone from dfsc.${ary[$i]};" |grep -v called_phone >>/tmp/select.txt;
done;