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%'

相关内容