shell读取mysql数据库两种方式

本文介绍了一种在SQL查询中使用动态参数的方法,并提供了两种不同的实现途径:通过循环遍历和直接存储结果的方式。这两种方法均涉及如何在命令行中灵活运用shell脚本技巧来动态替换SQL中的条件部分。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

代码中的动态参数大家可以自动过滤,直接查看获取的过程即可

第一种方式:循环遍历

mysql -hxxx -P3306 -uxxxx -pxxx test_bi -s -e \
"select m_ip,m_user,m_passwd,m_db,m_port,m_table,h_db,h_table,conditions from t_sqoop_task_info where is_valid=1 and task_id=67 limit 1" \
| while read m_ip m_user m_passwd m_db m_port m_table h_db h_table conditions

do
    m_conditions=${conditions//'$'dt/`echo $time`}
    echo ${m_ip}...${m_user}...${m_passwd}...${m_db}...${m_port}...${m_table}...${h_db}...${h_table}

    第二种方式:直接存结果
    read ct <<< $(echo `mysql -h$m_ip -P$m_port -u$m_user -p$m_passwd -D$m_db -s -e "select count(*) ct from $m_table where $m_conditions"`)
    echo mysql -h$m_ip -P$m_port -u$m_user -p$m_passwd -D$m_db -s -e "select count(*) ct from $m_table where $m_conditions"
    echo -e "${h_db}\t${h_table}\t${ct}" >> $data_path
done
   

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值