Friday, 22 April 2016

How to extract data from MySQL table using shell script ?


1. Connect with root user :
mysql -u root -proot

2. Selecting the DB :
use dbname

3. Extract data from MySQL table using below command :
SELECT <fields> INTO OUTFILE '<file-name>'
[ FIELDS TERMINATED BY '<symbol>' ]
[ OPTIONALLY ENCLOSED BY '<symbol>' ]
FROM <table>
[ WHERE <condition> ] ;

Example
Extract data from table "userinfo" into file "tool_userinfo_table.txt" with comma separated values and some values enclosed by " "
SELECT * INTO OUTFILE 'tool_userinfo_table.txt'
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'
FROM userinfo;

No comments:

Post a Comment

Note: only a member of this blog may post a comment.