本文介绍了与ORM一起使用时的情况(SQLalchemy)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我将SQLAlchemy与ORM paragdim一起使用.我无法找到一种方法来进行CASE WHEN指令.我在网上找不到有关此信息.
I am using SQLAlchemy with the ORM paragdim. I don't manage to find a way to do a CASE WHEN instruction. I don't find info about this on the web.
有可能吗?
推荐答案
请参见函数以及文档页面上的更多示例.但它看起来像这样(链接的文档中的冗词):
See sqlalchemy.sql.expression.case function and more examples on the documentation page. But it would look like this (verbatim from the documentation linked to):
case([(orderline.c.qty > 100, item.c.specialprice),
(orderline.c.qty > 10, item.c.bulkprice)
], else_=item.c.regularprice)
case(value=emp.c.type, whens={
'engineer': emp.c.salary * 1.1,
'manager': emp.c.salary * 3,
})
编辑-1 :(回答评论)当然可以,请参见以下示例:
edit-1: (answering the comment) Sure you can, see example below:
class User(Base):
__tablename__ = 'users'
id = Column(Integer, primary_key=True, autoincrement=True)
first_name = Column(String)
last_name = Column(String)
xpr = case([(User.first_name != None, User.first_name + " " + User.last_name),],
else_ = User.last_name).label("full_name")
qry = session.query(User.id, xpr)
for _usr in qry:
print _usr.fullname
有关使用混合动力. >用于混合属性.
Also see Using a hybrid for an example of case
used in the hybrid properties.
这篇关于与ORM一起使用时的情况(SQLalchemy)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!