シェルスクリプトの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:00デフォルトでは、datetimeパラメーターに日付値のみを指定すると、SQLはそれを使用します。その日付以降のすべてのログを一覧表示するには、ワイルドカードを使用します。

次のように試す

posting_date LIKE '$date%'

おすすめ記事