from django.db import connection
from rest_framework.response import Response
# 查询
def search(date):
# 年
year = date[:3]
# 月
month = date[-2:]
# 获取当月第一天的星期和当月的总天数
first_day_weekday, month_range = calendar.monthrange(int(year), int(month))
# 开始日期
start_date = date + '01'
# 截止日期
end_date = date + str(month_range)
# DB连接
cursor = connection.cursor()
# 业绩查询
search_sql = 'SELECT tb.item1,ta.item2,' \
' ta.item3, ' \
' ta.item4, ' \
' ta.item5' \
' FROM B AS tb,A AS ta' \
' WHERE ta.key = tb.key ' \
' AND ta.date <= ' + end_date + ' AND ta.date >= ' + start_date + \
' GROUP BY tb.no ORDER BY tb.no '
cursor.execute(search_sql )
return_arr = cursor.fetchall()
# 传回数据,cursor.fetchall后的结果集不带字符键,只能以下标取值
result = []
for item in return_arr:
app_result = {
'item1': item[0],
'item2': item[1],
'item3': item[2],
'item4': item[3],
'item5': item[4]
}
result.append(app_result)
return Response({'result': result})