Shell-Script

sql查詢shell腳本中的動態日期

  • April 15, 2020

好的,請參考下面的程式碼,然後我會提出我的問題。

#!/usr/bin/bash

#scirpt to connect with db

master_db_user='root'
master_db_passwd='123'
master_db_port='3306'
master_db_host='localhost'
master_db_name='uppcldashboard'

Now=$(date +"%d-%m-%Y")
date=$(date +"%F")


#Preparing script

#SQL_Query='select * from test_table;'

#MySql Command to connect to a database

#mysql -u$master_db_user -p$master_db_passwd -D$master_db_name -e "select * from uppcl_amount_details into outfile '/created_files/uppcl.csv' fields terminated by ',' lines terminated by '\n';"

mysql -u$master_db_user -p$master_db_passwd -D$master_db_name -e "select * from uppcl_amount_details where posting_date='$date' into  outfile '/created_files/uppcl.csv' fields terminated by ',' lines terminated by '\n';"

mv /created_files/uppcl.csv /created_files/offline_collection$Now.csv

sed -i "1i id","Cashier_id","Cheque_no","collection_amount","collection_type","Posting_date","Discom","Division_Code","Division_Name","Head_cashier_id","ref_3","ref_4","ref_5","ref_6","ref_7","ref_8","Tran_Date","Status","Posting_Updated_Date" /created_files/offline_collection$Now.csv
echo "End of the Script"

好的,所以我能夠獲取日期為 2020-04-15 00:00:00 但我無法獲取日期為 2020-04-15 12:12:10 的數據。

所以我在想是否有一種方法可以以 %Y-%m-%d 格式從數據庫中獲取數據,然後使用我的日期變數($Now)來提取所有數據。

有沒有什麼辦法

盡快尋求幫助。

謝謝和問候, 薩加爾曼達爾

預設情況下,2020-04-15 00:00:00如果我們只為 datetime 參數提供日期值,則 sql 將採用。使用萬用字元列出該日期的所有日誌。

試試看

posting_date LIKE '$date%'

引用自:https://unix.stackexchange.com/questions/580151