I have two tables "tags" and "deal_tag", and table definition follows,
我有两个表“tags”和“deal_tag”,表格定义如下,
Table('tags', metadata,
Column('id', types.Integer(), Sequence('tag_uid_seq'),
primary_key=True),
Column('name', types.String()),
)
Table('deal_tag', metadata,
Column('dealid', types.Integer(), ForeignKey('deals.id')),
Column('tagid', types.Integer(), ForeignKey
('tags.id')),
)
I want to select tag id, tag name and deal count (number of deals per tag). Sample query is
我想选择标签ID,标签名称和交易计数(每个标签的交易数量)。示例查询是
SELECT tags.Name,tags.id,COUNT(deal_tag.dealid) FROM tags INNER JOIN
deal_tag ON tags.id = deal_tag.tagid GROUP BY deal_tag.tagid;
How do I create the above query using SqlAlchemy select & join functions?
如何使用SqlAlchemy select&join函数创建上述查询?
2 个解决方案
#1
Give this a try...
尝试一下......
s = select([tags.c.Name, tags.c.id, func.count(deal_tag.dealid)],
tags.c.id == deal_tag.c.tagid).group_by(tags.c.Name, tags.c.id)
#2
you can join table in the time of mapping table
你可以在映射表的时候连接表
in the orm.mapper()
在orm.mapper()中
for more information you can go thru the link
有关更多信息,您可以通过链接
www.sqlalchemy.org/docs/
#1
Give this a try...
尝试一下......
s = select([tags.c.Name, tags.c.id, func.count(deal_tag.dealid)],
tags.c.id == deal_tag.c.tagid).group_by(tags.c.Name, tags.c.id)
#2
you can join table in the time of mapping table
你可以在映射表的时候连接表
in the orm.mapper()
在orm.mapper()中
for more information you can go thru the link
有关更多信息,您可以通过链接
www.sqlalchemy.org/docs/