shell脚本怎么读取mysql结果集的值?

发布时间:2020-10-14编辑:脚本学堂
在linux下用shell脚本读取mysql结果集中值的二种方法,按行读取sql结果,将sql执行结果读取到shell变量中,然后读取。

1、shell/ target=_blank class=infotextkey>shell脚本,按行读取sql结果:
 

while read -a row
do
    echo "..${row[0]}..${row[1]}..${row[2]}.."
done < <(echo "SELECT A, B, C FROM table_a" | mysql database -u $user -p $password)

2、shell脚本,将sql执行结果读取到shell变量中:
 

while read a b c
do
    echo "..${a}..${b}..${c}.."
done < <(echo "SELECT A, B, C FROM table_a" | mysql database -u $user -p $password)