我尝试在SQLAlchemy(ORM)中执行以下请求:
SELECT id,ref_prog FROM stepvand_1c_1t.equipment_day_hour WHERE id IN(SELECT id FROM stepvand_1c_1t.equipment WHERE equipment_type ='L')
我做了:
subq = session.query(Equipment)
subq = subq.filter(Equipment.equipment_type == "L").subquery()
query = session.query(EquipmentDayHour)
query = query.filter(EquipmentDayHour.id.in_(subq))
但这不起作用......
Python告诉我子请求有太多列。
答案 0 :(得分:1)
我认为您应该只更改示例代码的一行:
# error: includes all columns of Equipment
`subq = session.query(Equipment)`
# correct: include only ID column
`subq = session.query(Equipment.id)`
但是,我相信你可以在没有子查询的情况下做到这一点:
query = (session.query(EquipmentDayHour).
# version-1: if you have a relationship between EquipmentDayHour and Equipment
join(Equipment).
# version-2: if you do not have such relationship
#join(Equipment, EquipmentDayHour.id==Equipment.id).
filter(Equipment.equipment_type == "L")
)