shell连接mysql 取出结果放到文件里

本文介绍 MySQL 命令行工具中 -e 参数的功能和用法,并通过实例展示了如何利用 -e 参数来执行 SQL 语句。此外还提到了 -s 参数的作用。

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

mysql -h1.4.4.44 -uroot -p123table --default-character-set='utf8'-N -e "select vid from video where id=$i" >> iid.txt;

执行指定的sql语句


mysql的-e参数


参数解释
  1. --execute=statement, -e statement  
  2. xecute the statement and quit. The default output format is like that produced with --batch  
  1. --silent, -s  
  2. Silent mode. Produce less output. This option can be given multiple times to produce less and less output.  

示例代码
  1. select_sql="select count(distinct id) from tb_test"  
  2. num=$(mysql -s -h$host -u$user -p$passwd $dbname -e "$register_sql")  

注意:
  • -s参数的使用是减少查询字段的输出(ps:我这里只需要查询的结果值,并不需要查询的字段名,不加-s参数会输出查询的字段名)

管道运算符

  1. echo "select count(distinct id) from tb_test" | mysql -h$host -u$user -p$passwd $dbname  

ps:我建议使用-e参数,更加方便吧

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值