shell 獲取MySQL查詢結果並處理

主要應用到shell for循環

定義數據庫鏈接信息

HOST_NAME='127.0.0.1'
DB_PORT='3306'
DB_NAME='數據庫名'
USER_NAME='root'
PASSWD='root'mysql

TIME 當前時間戳 $() 注意date中間是有空格的

TIME=$(date '+%s')linux

-s 去掉表頭

MYSQL_ETL="mysql -h${HOST_NAME} -P${DB_PORT} -u${USER_NAME} -p${PASSWD} ${DB_NAME} -s -e"
hive_table_sql="select user_id from mx_user where token_time >0 and online=1 and token_time <= ${TIME}"
hive_table=$($MYSQL_ETL "${hive_table_sql}")sql

for 變量 in 查出的數據 ,而後遍歷這個變量 作 處理

for userid in $hive_table
do
此處邏輯處理(我這裏的例子是查出再修改)
update_sql="update mx_user set online=0 where user_id=${userid}"
$($MYSQL_ETL "${update_sql}")
doneshell

我當時在Windows編寫的腳本,而後放到Linux裏面直接執行會報錯,緣由是windows 的回車空格與linux的空格格式是不同的,方法不少也很簡單,藉助工具submit_text編輯器裏view > line engings 將Windows修改成Uinx保存便可!方法不少,這裏一種可做參考,其餘方法再本身百度。

相關文章
相關標籤/搜索