如何使用if语句选择peewee
问题描述:
我正在获取所有类别并加入具有帖子类别的item_category的类别表。 我的SQL就是这样如何使用if语句选择peewee
SELECT category.*,IF(item_category.category_id=NULL, 0,1) is_selected
FROM category
LEFT JOIN item_category ON category.category_id=item_category.category_id AND item_id=1
我试过,但没有奏效。因为如果使用python抛出错误如果
Category.select(Category, ItemCategory, fn.if(ItemCategory.category_id==None, 0, 1))
if语句的正确用法是什么? 谢谢。
答
我找到了一个解决方案,但我不知道这是最好的方法。 我改变“fn.if”到“fn.IF”和它的工作
Category.select(Category, ItemCategory, fn.IF(ItemCategory.category_id==None, 0, 1))
我不熟悉'IF',但你也可以使用'case'。有关详细信息,请查看案例文档:http://docs.peewee-orm.com/en/latest/peewee/playhouse.html#case – coleifer 2014-10-20 15:37:31
@coleifer谢谢。你可以找到关于“mysql if”的信息http://dev.mysql.com/doc/refman/5.0/en/control-flow-functions.html#function_if – Alexander 2014-10-20 15:41:47