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

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

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

#!/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)來提取所有數據。

有什麼辦法嗎

盡快需要協助。

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

答案1

2020-04-15 00:00:00預設情況下,如果我們只為日期時間參數提供日期值,則sql 將採用。使用通配符列出該日期以來的所有日誌。

嘗試作為

posting_date LIKE '$date%'

相關內容