Sqlalchemy 執(zhí)行sql 語(yǔ)句

        要求:過期(分為庫(kù)存為0和超出有效時(shí)間)的商品排在下面,在售狀態(tài)的商品排在上面。之后再分別按照發(fā)布時(shí)間排序。
        Sql語(yǔ)句如下:
        select deadline, total_count, c_time  from (
            select *, 0 as ord from sale_goods where goods_type = 'sale-goods' and delete = false and total_count > 0 and deadline > now()
            union
            select *, 1 as ord from sale_goods where goods_type = 'sale-goods' and delete = false and (total_count <= 0 or deadline <= now())
        ) sale_goods_mall order by ord asc, c_time desc;
        :return:
        # 分頁(yè)
        pages = get_arg('pages', u'頁(yè)碼', default=1, parm_type=int)
        limit = get_arg('limit', u'條數(shù)', default=30, parm_type=int)
        
        # 總的數(shù)據(jù)sql
        sql_str = "select * from (" \
                  "select *, 0 as ord from sale_goods where goods_type = 'sale-goods' and delete = false and total_count > 0 and deadline > now()" \
                  "union " \
                  "select *, 1 as ord from sale_goods where goods_type = 'sale-goods' and delete = false and (total_count <= 0 or deadline <= now())" \
                  ") sale_goods_mall order by ord asc, c_time desc limit %d offset %d;" % (limit, limit * (pages - 1))

        # 在售的數(shù)據(jù)sql
        in_sale_sql = "select *, 0 as ord from sale_goods where goods_type = 'sale-goods' and delete = false and total_count > 0 and deadline > now();"
      
        # sqlalchemy執(zhí)行sql
        data_query = db.session.execute(sql_str)
        in_sale_query = db.session.execute(in_sale_sql)
        
        # 獲取查詢到的數(shù)據(jù)條數(shù)
        total = data_query.rowcount
        sale_total = in_sale_query.rowcount
        
        # fetchall()遍歷到所有數(shù)據(jù)
        data = {
            'total': total,
            'sale_total': sale_total,
            'sale_goods': [{
                'deadline': time_to_str(sale_goods['deadline'], only_date=True) or '',
                'id_no': sale_goods['id_no'] or '',
                'c_time': time_to_str(sale_goods['c_time'], only_date=True),
                'deadline_invalid': True if float(time.mktime(sale_goods['deadline'].timetuple())) < float(time.time()) else False
            }for goods in data_query.fetchall()]
        }
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容