본문 바로가기

Database/MySQL

MySQL 쿼리로 csv 데이터 추출하기

tablelist="
T1
T2
T3
"

HOST=127.0.0.1
PASSWD=pass
DATABASE=test

CMD="mysql -h$HOST -uroot -p$PASSWD $DATABASE -sN -e "

map[T1]="'BTC','ETH'"
map[T2]="'BTC2','ETH2'"
map[T3]="'BTC3','ETH3'"


START_TIME="unix_timestamp( '2021-09-27' )"
END_TIME="unix_timestamp( '2021-09-29' )"


function exportDiffData()
{
   set -f
   SQL="SELECT * FROM $1 where $START_TIME <= unix_time and unix_time < $END_TIME and symbol in ( ${map[${1}]} )"
   echo $SQL
   $CMD "$SQL" | sed 's/\t/,/g' > $1.csv
   set +f
}



for table in $tablelist
do
   echo $table
   exportDiffData ${table}
done