2019-11-16使用python连接sql

# 导入相关库
%matplotlib inline
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from dateutil.parser import parse
plt.rcParams["font.sans-serif"]='SimHei'#解决中文乱码
plt.rcParams['axes.unicode_minus'] = False#解决负号无法正常显示的问题
# 连接数据库
from sqlalchemy import create_engine
from sqlalchemy import text 
import pandas as pd
engine=create_engine('mysql+pymysql://')
# 统计了WDGC超市2017年8月在售商品的总订单数,并降序排列
sql='''SELECT substring(t.SDate,1,6) as YearMonth
      ,t.ShopID
      ,t.GoodsID
      ,t1.GoodsName
      ,count(DISTINCT t.sheetID) as order_number
from OrderItem t
JOIN Goods t1 on t.GoodsID = t1.GoodsID
where t.SDate between '20170801' and '20170831'
and t.ShopID='WDGC'
GROUP BY substring(t.SDate,1,6)
        ,t.ShopID
        ,t.GoodsID
        ,t1.GoodsName
order by count(DISTINCT t.sheetID) desc;'''
df=pd.read_sql_query(sql,engine)
df.head()
image.png

通过df.info()可以看出有8月份有7867个商品ID在出售

你可能感兴趣的:(2019-11-16使用python连接sql)