shell脚本循环执行mysql语句
2015-06-08 19:50
686 查看
参考资料:Shell脚本中执行mysql语句
需求:数据库里有张数据表存储的是用户对电影的评价(user_id movie_id rating time),但是我现在要每部电影的总评分。
解决方法:
1)、写个sql文件test.sql:use movie_recommendation;select distinct movie_id from T_user_movie_rating_map;
2)、在终端输入mysql -uroot -p123456 -e < test.sql > movie_id.txt
这样就会获得电影id的列表,然后编辑该文件把第一行的movie_id这个字符串删掉
3)、然后编辑如下shell脚本rating.sh:
4)、在终端输入. ./rating.sh即可将电影id和电影对应的评分写入到指定文件夹下
说明:中间对result这个结果进行了处理,是因为查询数据库获得的结果有标题,需要去掉这个标题
需求:数据库里有张数据表存储的是用户对电影的评价(user_id movie_id rating time),但是我现在要每部电影的总评分。
解决方法:
1)、写个sql文件test.sql:use movie_recommendation;select distinct movie_id from T_user_movie_rating_map;
2)、在终端输入mysql -uroot -p123456 -e < test.sql > movie_id.txt
这样就会获得电影id的列表,然后编辑该文件把第一行的movie_id这个字符串删掉
3)、然后编辑如下shell脚本rating.sh:
#!/bin/bash for line in $(cat movie_id.txt) do result=$(mysql -uroot -p123456 -e "use movie_recommendation;select avg(rating) from T_user_movie_rating_map where movie_id=$line") tmp=$(echo $result | sed 's/ /\n/g') for tmp_line in $tmp do result=$tmp_line done echo $line:$result echo $line,$result >> rating.txt done
4)、在终端输入. ./rating.sh即可将电影id和电影对应的评分写入到指定文件夹下
说明:中间对result这个结果进行了处理,是因为查询数据库获得的结果有标题,需要去掉这个标题
相关文章推荐
- 数据需求统计常用shell命令---AWK分组求和,分组统计次数
- 数据需求统计常用shell命令
- 关于cron和crond,crontab以及bash脚本的写法等知识 cron的语法用法
- 常用的shell脚本
- 常用的shell脚本
- 用shell写的nginx服务程序
- 1005--HBase操作实战(HBase Shell命令行模式)
- xshell配置
- shell输入与输出
- CentOS下编写shell脚本自动备份数据库与网站
- adb shell am 的用法
- shell学习第五天--基本的I/O重定向
- Shell :利用 sshpass 远程传输文件
- linux shell数据重定向(输入重定向与输出重定向)详细分析
- linux shell 数组建立及使用技巧
- linux shell 自定义函数(定义、返回值、变量作用域)介绍
- linux shell 流程控制(条件if,循环【for,while】,选择【case】语句实例
- Shell :导出 Mysql 数据
- linux shell 字符串操作(长度,查找,替换)详解
- 如何不重起而让bashrc的修改生效——使用source命令