我有一个包含表的特定列值的文件,例如。 file.txt 有
1个
2个
3个
...
在 shell 脚本中,我用这个值循环选择语句
文件=$1
对于 cat $file
中的行
做
select * into outfile '/path/filename.txt' where colvalue='$line';
现在,我想要一种将每个值(即 1、2、3)的输出写入同一个文件的方法。在理想情况下,它会抛出错误“文件已存在”。
最佳答案
尝试这样的事情: 排序和 uniq 删除重复条目 sed 将 annd 并引用 args tr -d 将删除换行符(仅适用于光学)
我的输入文件
$ cat n.txt
1
3
5
66
99
2
生成查询
$ echo "select * into outfile '/path/filename.txt' where colvalue in (" > query.sql
$ sort n.txt | uniq |sed -e "s/^\(.*\)$/'\1'/" -e '2,$s/^/,/g' | tr -d '\n' >> query.sql
$ echo ");" >> query.sql
显示查询(仅用于验证)
$ cat query.sql
select * into outfile '/path/filename.txt' where colvalue in (
'1','2','3','5','66','99');
运行查询
$ mysql -u root < query.sql
关于mysql - 有没有办法根据 where 条件下的数据值从 mysql 中的单个选择查询中获取多个输出文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41827781/