I am trying to dump an output from from a MYSQL view into a CSV using a bash script.
As inspiration I used this post: How to use a bash script to write to a mysql table
The strange part is that SELECT column1,column2,column3 FROM syntax works fine, but it is not practical when you have some 15 columns…
Also SELECT count(*) FROM works
However SELECT * FROM is throwing a MYSQL syntax error
#!/bin/bash
fpath=/tmp/
fname=`date +%Y%m%d-%H%M%S`.csv
savedest=\'$fpath$fname\'
echo "Saving into"$savedest
params="-u root -p mydb"
s1="SELECT * FROM flat_view INTO OUTFILE "
s2=" FIELDS TERMINATED BY ','"
s3=" ENCLOSED BY '\"'"
s4=" LINES TERMINATED BY '\\n'"
selectend=";"
echo $s1$savedest$s2$s3$s4$selectend | mysql $params
I am thinking I have not escaped something properly.
PS. This is Centos 5.8 and mysql Ver 14.12 Distrib 5.0.95
While typing up the question I decided to try one more thing for laughs:
and it worked!
I found a reference to a possible ambiguity of SELECT * reading MySQL documentation: http://dev.mysql.com/doc/refman/5.5/en/select.html
Still, it is strange that SELECT * FROM worked fine from mysql command line, but not from a bash script.