温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

python运行mysql语句报错怎么解决

发布时间:2022-04-25 10:54:39 来源:亿速云 阅读:387 作者:iii 栏目:大数据

这篇“python运行mysql语句报错怎么解决”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇“python运行mysql语句报错怎么解决”文章吧。

一  python 运行mysql 语句报错

众所周知,python有转译机制 %s和%d都会被转译成字符串或数字,而sql的模糊查询也需要用到%,都进行模糊查询时,刚好查询条件还是个变量那就很尴尬了。

解决方法其实很简单,把需要进行模糊查询的字符串从sql中单独拎出来进行拼接就好

错误方式

shopId = "zbw_010002"
'''select * from base_saleflows where shopId='{0}' and card_id is not NULL and standard_cls4 like "%湿巾%" '''.format(shopId)

发现 不对,这样的语句 mysql是运行不了的。

python运行mysql语句报错怎么解决

args='%湿巾%'shopId = "zbw_010002"mysql_sentence='''select a.shopId, a.sale_money,a.card_id ,a.standard_cls4 from base_saleflows a join  base_vips b on a.card_id = b.card_id where a.shopId='{0}' and a.card_id is not NULL and a.standard_cls4 like '{1}' '''.format(shopId,args)print(mysql_sentence)

结果为

select * from base_saleflows a join  base_vips b on a.card_id = b.card_id where a.shopId='zbw_010002' and a.card_id is not NULL and a.standard_cls4 like '%湿巾%'

二  字符串分组拼接

对 cls3列 按照流水号flow_no 进行 分组拼接字符串,拼接符号为‘-’

#  分组拼接result = vipsaleflow_common.pivot_table(values='standard_cls3',index='flow_no',aggfunc=lambda x:x.str.cat(sep='-'))

三  每个月  、季度 新客数

  编写函数

def new_count(saleflow, vip_col_name =['card_id'] , groupby_list=['year','month'], shopId=shopId):    """    会员流水新客数    """    first =saleflow.groupby(vip_col_name).oper_date.min().reset_index().\    rename(columns={'oper_date':'oper_date_first'})    first["month"] = first.oper_date_first.map(lambda x: x.month)    first["year"] = first.oper_date_first.map(lambda x: x.year)    lookup = {1: 1, 2:1,3:1,4:2,5:2, 6:2, 7:3,8:3,9:3,10:4, 11:4,12:4}    first['season'] = first.oper_date_first.apply(lambda x: lookup[x.month])    first['YearMonth'] = first.oper_date_first.map(lambda x: 100*x.year + x.month)
   monthly_new_count = first.groupby(groupby_list).card_id.nunique()\    .reset_index().rename(columns={'card_id':'card_id'+'_nunique'})        return monthly_new_count

应用  :按照 年  ,月 ,  分店号   ,湿巾类型)分组 求 每月 各个湿巾类型的新客数

## 每个月RESULT_month =new_count(saleflow, vip_col_name = ['branch_no','card_id','shijing_class'] ,             groupby_list=['year','month','branch_no','shijing_class'], shopId=shopId)

python运行mysql语句报错怎么解决

应用 :按照 ( 年  ,季度 ,  分店号   ,湿巾类型)   分组 求每个季度新客数

四  根据时间戳衍生 年 月  季度

saleflow['oper_date']=saleflow['oper_date'].astype(str)  #字符串saleflow['oper_date'] = saleflow.oper_date.apply(lambda x:datetime.strptime(x, '%Y-%m-%d %H:%M:%S'))saleflow["month"] = saleflow.oper_date.map(lambda x: x.month)saleflow["year"] = saleflow.oper_date.map(lambda x: x.year)#https://www.it1352.com/584941.htmllookup = {1: 1, 2: 1,3: 1,4: 2, 5: 2, 6: 2, 7: 3,8: 3,9: 3,10: 4, 11: 4,12: 4}saleflow['Season'] = saleflow['oper_date'].apply(lambda x: lookup[x.month]) saleflow['YearMonth'] = saleflow['oper_date'].map(lambda x: 100*x.year + x.month)

以上就是关于“python运行mysql语句报错怎么解决”这篇文章的内容,相信大家都有了一定的了解,希望小编分享的内容对大家有帮助,若想了解更多相关的知识内容,请关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI