How to input data into user defined variables into MySql query

Posted by user292791 on Programmers See other posts from Programmers or by user292791
Published on 2014-06-12T06:31:31Z Indexed on 2014/06/12 9:39 UTC
Read the original article Hit count: 202

Filed under:
|
|

Simple Shell script

echo "Enter 1 for month of March"
echo "Enter 2 for month of April"
echo "Enter 3 for month of May"

read Month

case "$Month" in

1)

echo "enter establishment name"
read a;

mysql -u root -p $a < "March.sql";;

2)

echo "enter establishment name"
read b;

mysql -u root -p $b < "April.sql";;

3)

echo "enter establishment name"
read c;

mysql -u root -p $c < "May.sql";;

esac

done

In this i have three other query files March.sql, April.sql, May.sql. i'm linking this in shell script .

Example of .sql file:

SELECT DISTINCT substr(
a.case_no, 3, 2
), b.case_type, b.type_name, a.case_no into outfile '/tmp/April.csv' 
FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY '\r\n' 
FROM Civil_t AS a, Case_type_t AS b, disposal_proc AS c
WHERE substr(
a.case_no, 3, 2
) = b.case_type
AND a.date_of_decision
BETWEEN  '2014-04-01'
AND  '2014-04-30'
AND a.case_no = c.case_no
AND a.court_no =1;

I have to alter the .sql script every time. Is there any method to read the variables from shell script and use it in mysql. For example:-

echo "enter date" read a #input date Now i have read a "date" and i want to use it in March.sql query in where clause. Is there is any method of using this variable in .sql query.

© Programmers or respective owner

Related posts about mysql

Related posts about scripting