SQL クエリ シェル スクリプトの動的日付

SQL クエリ シェル スクリプトの動的日付

わかりました。以下のコードを参照してから質問を投稿します。

#!/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:00datetime パラメータに日付値のみを指定すると、SQL はそれを取得します。ワイルドカードを使用して、その日付からのすべてのログを一覧表示します。

試してみてください

posting_date LIKE '$date%'

関連情報