mysql - 有没有办法根据 where 条件下的数据值从 mysql 中的单个选择查询中获取多个输出文件?

标签 mysql linux

我有一个包含表的特定列值的文件,例如。 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/

相关文章:

linux - 无法启动服务 prometheus : oci runtime error: container_linux. go:235: 启动容器进程导致 "container init exited prematurely"

linux - Linux 中读取文件

linux - Perl 模拟 linux 时间命令

linux - VM 无法通过 NAT ping 通由两个交换机和一个路由器组成的主机

php - 使用单选按钮和复选框过滤 HTML 表格

javascript - 双击更新传单 map 上的位置标记

php - PHP 和 Linux 更好地支持 CURL

php - Cron 作业错过了 wget 上的 JSON get 请求

mysql - 以mysql日期格式存储时区

linux conda 安装 log4j-2.10.0 包问题