如何使用Python在SQL查询中自动化日期

时间:2017-10-09 11:43:41

标签: python mysql pandas

我有SQL连接并命名为db_connection我每周运行

tran= pd.read_sql('SELECT order_id, p.user_id, p.barcode, title, o.timestamp, amount, p.name, qty FROM transactions t left join orders o on t.order_id = o.id left join products p on t.product_id = p.id where date(o.timestamp)<=\'2017-09-27\' and date(o.timestamp)>=\'2017-09-21\' and p.user_id = \'63165\' ', con=db_connection)

tran2= pd.read_sql('SELECT order_id, p.user_id, p.barcode, title, o.timestamp, amount, p.name, qty FROM transactions t left join orders o on t.order_id = o.id left join products p on t.product_id = p.id where date(o.timestamp)<=\'2017-09-27\' and date(o.timestamp)>=\'2017-09-21\' and p.user_id = \'62345\' ', con=db_connection)

...

tran22= pd.read_sql('SELECT order_id, p.user_id, p.barcode, title, o.timestamp, amount, p.name, qty FROM transactions t left join orders o on t.order_id = o.id left join products p on t.product_id = p.id where date(o.timestamp)<=\'2017-09-27\' and date(o.timestamp)>=\'2017-09-21\' and p.user_id = \'78345\' ', con=db_connection)

例如,我想将所有日期更改为date(o.timestamp)<=\'2017-10-04\' and date(o.timestamp)>=\'2017-09-28\',但这需要时间并且容易出错。是否有任何解决方案使日期只被调用一次?

1 个答案:

答案 0 :(得分:1)

的Python

虚拟解决方案是使用占位符以便编辑单个条目:

start = # evaluate or hardcode date
end = # evaluate or hardcode date

tran = pd.read_sql('... where date(o.timestamp)<=\'{END}\' and date(o.timestamp)>=\'{START}\' ...'.format(START=start, END=end), con=db_connection)
...
tran1 = ...

熊猫的参数

start = # evaluate or hardcode date
end= # evaluate or hardcode date
tran = pd.read_sql('... where date(o.timestamp)<=%(end)s and date(o.timestamp)>=%(start)s ...',
               con=db_connection, params={"start":start, "end":end})

CURDATE

使用函数CURDATE可以参数化查询。例如,如果您每周一运行查询并希望提取上周(周一至周日)的数据,则可以设置条件:

WHERE DATE(o.timestamp)<=(CURDATE() - INTERVAL 1 DAY) and DATE(o.timestamp)>=(CURDATE() - INTERVAL 8 DAY)

这显然取决于运行查询时 ,但如果调度程序自动提取,则效果很好。

或者,将它与DAYOFWEEK结合起来,我们可以让它独立于它运行的那一天(再次:考虑上周一到周日)):

WHERE DATE(o.timestamp) >= (CURDATE() - INTERVAL (DAYOFWEEK(CURDATE())+5) DAY) AND DATE(o.timestamp) <= (CURDATE() - INTERVAL (DAYOFWEEK(CURDATE())-1) DAY)

显然,你必须根据自己的需要调整日偏差。