我尝试在SQL查询中使用Jupyter notebook中的date_format来获取正确的数据,但Jupyter抛出了以下错误:
sql = "SELECT DATE_FORMAT(issues.created_on, "%Y") as YEAR, issues.tracker_id as Ticketgrund, count(*) FROM issues where issues.project_id = '2' group by YEAR, Ticketgrund;"
^
SyntaxError: invalid syntax"
这是我的手机号码:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import sqlalchemy
session = sqlalchemy.create_engine("mysql+pymysql://user:pwj@ip/DB")
sql = "SELECT DATE_FORMAT(issues.created_on, "%Y") as YEAR, issues.tracker_id as Ticketgrund, count(*) FROM issues where issues.project_id = '2' group by YEAR, Ticketgrund;"
df = pd.read_sql_query(sql,session)
df.head(20)
SQL查询运行时没有错误,但在Jupyter中没有。转义%(我假设这是错误的原因)不会改变任何事情。
我能怎么办?
1条答案
按热度按时间nukf8bse1#
用%转义%(并在双引号内使用单引号):
或者,使用SQLAlchemy中提供的date_format函数。下面是一个简短的示例: