【Python】执行SQL报错

可以再数据库查询界面执行的SQL,一直报错

unsupported format character 'Y' (0x59) at index 61

SQL如下:

data=pd.read_sql_query(sql="""select
                            date_format(create_time,'%Y-%m') as mon
                            ,count(distinct order_id) as ord_cnt
                            from prod.order_info
                            where date_format(create_time,'%Y-%m') between '2022-01' and '2022-12'
                            group by date_format(create_time,'%Y-%m')
                            order by date_format(create_time,'%Y-%m')"""
                     ,con=con,index_col=['mon'])

'Y'那里前面是有%的,才想起来%在Python里是有特殊含义的字符,所以要用其他方式处理掉,比较简单的方法是用 '%%' 代替 '%',这样就是一个单纯的百分号了

data=pd.read_sql_query(sql="""select
                            date_format(create_time,'%%Y-%%m') as mon
                            ,count(distinct order_id) as ord_cnt
                            from prod.order_info
                            where date_format(create_time,'%%Y-%%m') between '2022-01' and '2022-12'
                            group by date_format(create_time,'%%Y-%%m')
                            order by date_format(create_time,'%%Y-%%m')"""
                     ,con=con,index_col=['mon'])

正常执行成功!

【Python】执行SQL报错_第1张图片

 

你可能感兴趣的:(sql,数据库,oracle,python,mysql)