【发布时间】:2012-11-13 18:33:25
【问题描述】:
我有一个包含 3 列的表:类型、内容和时间(一个整数)。对于每个“类型”,我想选择具有最大(最近)“时间”整数和相应数据的条目。如何使用 SQLAlchemy 和 Python 做到这一点?我可以通过执行以下操作使用 SQL 来做到这一点:
select
c.type,
c.time,
b.data
from
parts as b
inner join
(select
a.type,
max(a.time) as time
from parts as a
group by a.type) as c
on
b.type = c.type and
b.time = c.time
但是我怎样才能在 SQLAlchemy 中做到这一点呢?
表映射:
class Structure(Base):
__tablename__ = 'structure'
id = Column(Integer, primary_key=True)
type = Column(Text)
content = Column(Text)
time = Column(Integer)
def __init__(self, type, content):
self.type = type
self.content = content
self.time = time.time()
def serialise(self):
return {"type" : self.type,
"content" : self.content};
尝试的查询:
max = func.max(Structure.time).alias("time")
c = DBSession.query(max)\
.add_columns(Structure.type, Structure.time)\
.group_by(Structure.type)\
.subquery()
c.alias("c")
b = DBSession.query(Structure.content)\
.add_columns(c.c.type, c.c.time)\
.join(c, Structure.type == c.c.type)
给我:
sqlalchemy.exc.OperationalError: (OperationalError) 靠近 "(": 语法 错误 u'SELECT structure.content AS structure_content, anon_1.type AS anon_1_type, anon_1.time AS anon_1_time \nFROM 结构 JOIN (SELECT time.max_1 AS max_1, structure.type AS 类型, structure.time AS 时间 \nFROM max(structure.time) AS 时间,结构 GROUP BY structure.type) AS anon_1 ON structure.type = anon_1.type' ()
我基本上是在黑暗中刺伤,所以任何帮助将不胜感激。
【问题讨论】:
-
这可以使用 SQLAlchemy 来完成。你已经尝试过什么了吗?您是否定义了任何表映射?
-
@MartijnPieters 嘿 Martijn,我昨晚在离开办公室的路上发布了这篇文章。一旦我有机会再看一遍,我会添加一些代码;谢谢。
-
@MartijnPieters 到目前为止我已经添加了我的工作,但不知道我是否走在正确的轨道上。
标签: python sqlalchemy