【发布时间】:2021-03-16 14:15:17
【问题描述】:
我有一个这样的代码 sn-p。我对 %()s 的使用感到困惑。 如果用于格式化参数, 我们不应该使用 %(threshold)d 作为阈值,因为它是一个数字吗?
def recommendations_for_user(user_id, threshold=4.5):
# Join with the courses table
query = """
SELECT title, rating FROM recommendations
INNER JOIN courses ON courses.course_id = recommendations.course_id
WHERE user_id=%(user_id)s AND rating>%(threshold)s
ORDER BY rating DESC
"""
# Add the threshold parameter
predictions_df = pd.read_sql(query, db_engine, params = {"user_id": user_id,
"threshold": threshold})
return predictions_df.title.values
# Try the function you created
print(recommendations_for_user(12, 4.65))
【问题讨论】:
-
这不是(仅)SQL。 Edit 问题并标记宿主语言和/或框架。
-
您使用的是哪个 dbms?