Shell script Loop executes MySQL statement

Source: Internet
Author: User

Reference: Executing MySQL statements in shell scripts

requirement : There is a data table in the database that stores the user's comments on the movie (user_id movie_id rating time), but I'm going to have a total score for each movie.

Workaround :

1), write a SQL file Test.sql:use movie_recommendation;select distinct movie_id from T_user_movie_rating_map;

2), in terminal input MYSQL-UROOT-P123456-E < Test.sql > Movie_id.txt

This will get a list of movie IDs, and then edit the file to delete the movie_id string from the first line.

3), and then edit the following shell script rating.sh:

1#!/bin/Bash2 3  forLineinch$(Catmovie_id.txt)4  Do5result=$ (mysql-uroot-p123456-e"Use movie_recommendation;select avg (rating) from T_user_movie_rating_map where movie_id= $line")6tmp=$ (Echo$result |sed 's//\n/g')7      forTmp_lineinch$tmp8      Do9result=$tmp _lineTen      Done One     Echo$line: $result A     Echo$line, $result >>Rating.txt -  Done

4), in the terminal input. The./rating.sh can write the movie ID and movie corresponding score to the specified folder

Note: The result is processed in the middle, because the query database obtains the result has the title, needs to remove this title

Shell script Loop executes MySQL statement

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.