query = select([machine_products_table.c.id, /
machine_products_table.c.machine_id.label("kiosk"), /
machine_products_table.c.sale_price.label("sale_price"), /
machine_products_table.c.quantity.label("quantity"), /
select([func.min(machine_stock_events_table.c.action_time)], /
and_(machine_stock_events_table.c.machine_id==machine_products_table.c.machine_id, /
machine_stock_events_table.c.product_id==productId)).as_scalar().label("load_days"), /
select([func.max(machine_transactions_table.c.trs_time)], /
and_(machine_transactions_table.c.machine_id==machine_products_table.c.machine_id, /
machine_transactions_table.c.product_id==productId, /
machine_transactions_table.c.state == "closed")).as_scalar().label("no_transaction_days")/
], /
and_(machine_products_table.c.machine_id.in_(select([machines_table.c.machine_id], /
machines_table.c.client_id==clientId).as_scalar()), /
machine_products_table.c.product_id==productId, /
machine_products_table.c.quantity != 0))
if sortKey:
if str(sortOrder).lower() == "desc":
query = query.order_by(sortKey.desc())
else:
query = query.order_by(sortKey)
if limit:
query = query.limit(limit)
if offset:
query = query.offset(offset)
tmp = meta.Session.execute(query).fetchall()
result = []
for t in tmp:
load_days = '--'
no_transaction_days = '--'
if t[4]:
load_days = getDaySpan(str(t[4]),getCurTime())
if t[5]:
no_transaction_days = getDaySpan(str(t[5]),getCurTime())
result.append((t[0], t[1], t[2], t[3], load_days, no_transaction_days))
return result
子查询的使用
最新推荐文章于 2024-12-23 10:09:44 发布
本文介绍了一个复杂的SQL查询案例,该查询涉及多个表的联接操作、子查询以及条件过滤等高级特性。通过合理使用这些特性,可以有效地从数据库中获取所需的数据,并进行进一步的数据分析。文章还展示了如何对查询结果进行排序、限制返回行数以及偏移量的设置。
914

被折叠的 条评论
为什么被折叠?



