order_by,filter的语法。
用久了才会熟悉。
Session = sessionmaker(bind=engine) session = Session() print(session.query(Cookie.cookie_name, Cookie.quantity).first()) for cookie in session.query(Cookie).order_by(desc(Cookie.quantity)): print('{:3} - {}'.format(cookie.quantity, cookie.cookie_name)) # query = session.query(Cookie).order_by(Cookie.quantity)[:2] query = session.query(Cookie).order_by(Cookie.quantity).limit(4) print([result.cookie_name for result in query]) inv_count = session.query(func.sum(Cookie.quantity)).scalar() print(inv_count) rec_count = session.query(func.count(Cookie.cookie_name)).first() print(rec_count) rec_count = session.query(func.count(Cookie.cookie_name).label('inventory_count')).first() print(rec_count.keys()) print(rec_count.inventory_count) record = session.query(Cookie).filter(Cookie.cookie_name == 'chocolate chip').first() print record record = session.query(Cookie).filter_by(cookie_name='chocolate chip').first() print(record) query = session.query(Cookie).filter(Cookie.cookie_name.like('%chocolate%')) for record in query: print(record.cookie_name) results = session.query(Cookie.cookie_name, 'SKU-' + Cookie.cookie_sku).all() for row in results: print(row) query = session.query(Cookie.cookie_name, cast((Cookie.quantity * Cookie.unit_cost), Numeric(12, 2)).label('inv_cost')) for result in query: print('{} - {}'.format(result.cookie_name, result.inv_cost)) query = session.query(Cookie).filter( Cookie.quantity > 23, Cookie.unit_cost < 0.40 ) for result in query: print(result.cookie_name) query = session.query(Cookie).filter( or_( Cookie.quantity.between(10, 50), Cookie.cookie_name.contains('chip') ) ) for result in query: print(result.cookie_name)
输出:
('chocolate chip', 12) 100 - oatmeal raisin 24 - peanut butter 12 - chocolate chip 12 - chocolate chip 12 - chocolate chip 12 - chocolate chip 1 - dark chocolate chip 1 - molasses 1 - dark chocolate chip 1 - molasses ['dark chocolate chip', 'molasses', 'dark chocolate chip', 'molasses'] 176 (10,) ['inventory_count'] 10 Cookie(cookie_name='chocolate chip', cookie_recipe_url='http://www.baidu.com/', cookie_sku='CC01', quantity=12, unit_cost=0.50) Cookie(cookie_name='chocolate chip', cookie_recipe_url='http://www.baidu.com/', cookie_sku='CC01', quantity=12, unit_cost=0.50) chocolate chip chocolate chip chocolate chip dark chocolate chip chocolate chip dark chocolate chip ('chocolate chip', 'SKU-CC01') ('chocolate chip', 'SKU-CC01') ('chocolate chip', 'SKU-CC01') ('dark chocolate chip', 'SKU-CC02') ('molasses', 'SKU-MOL01') ('chocolate chip', 'SKU-CC01') ('dark chocolate chip', 'SKU-CC02') ('molasses', 'SKU-MOL01') ('peanut butter', 'SKU-PB01') ('oatmeal raisin', 'SKU-EWW01') chocolate chip - 6.00 chocolate chip - 6.00 chocolate chip - 6.00 dark chocolate chip - 0.75 molasses - 0.80 chocolate chip - 6.00 dark chocolate chip - 0.75 molasses - 0.80 peanut butter - 6.00 oatmeal raisin - 100.00 peanut butter chocolate chip chocolate chip chocolate chip dark chocolate chip chocolate chip dark chocolate chip peanut butter
时间: 2024-11-03 02:00:11