欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

sqlalchemy flask case when查询

程序员文章站 2024-03-03 18:02:52
...
from sqlalchemy import case
case_sqr = case([(Master.STATUS == 0, Master.SCS)], else_=Master.ZCS).label("SQR")
case_end = case([(Master.STATUS == 0, Master.BLQX)],else_=case([(Master.PZYC == '', Master.SQYC)],else_=Master.PZYC)).label("END")

pages = Master.query.with_entities(Master.AC,case_sqr, case_end).filter(Master.STATUS=1).order_by(Master.MASTERID.desc()).paginate(page_index, page_num, False)

rsp = dict()
items = list()
for one_item in pages.items:
    one_dict = dict()
    one_dict["AC"] = one_item[0]
    one_dict["SQR"] = one_item[1]
    one_dict["END"] = one_item[2]
    items.append(one_dict)
rsp['CUR_PAGE'] = pages.page
rsp['TOTAL_PAGE'] = pages.pages
rsp["ITEMS"] = items

说明:Master为一个数据库类对象,实际情况实际代替,后端用的flask实现,paginate在flask中可用,tornado中没有该方法