zoukankan      html  css  js  c++  java
  • shell脚本循环执行mysql语句

    参考资料: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:

     1 #!/bin/bash
     2 
     3 for line in $(cat movie_id.txt)
     4 do
     5     result=$(mysql -uroot -p123456 -e "use movie_recommendation;select avg(rating) from T_user_movie_rating_map where movie_id=$line")
     6     tmp=$(echo $result | sed 's/ /
    /g')
     7     for tmp_line in $tmp
     8     do
     9         result=$tmp_line
    10     done
    11     echo $line:$result
    12     echo $line,$result >> rating.txt
    13 done

    4)、在终端输入. ./rating.sh即可将电影id和电影对应的评分写入到指定文件夹下

    说明:中间对result这个结果进行了处理,是因为查询数据库获得的结果有标题,需要去掉这个标题

  • 相关阅读:
    组件基础
    css 手稿
    HTML手稿
    Vmstat命令监控Linux资源并将数据通过图形化方式显示
    JAVA---类和对象
    JAVA---Graphics2D类
    JAVA---数组
    JAVA---图形处理
    JAVA----日历源代码
    SQL常用语句大全
  • 原文地址:https://www.cnblogs.com/Shirlies/p/4561780.html
Copyright © 2011-2022 走看看