Convert SQL to SQL alchemy(将 SQL 转换为 SQL 炼金术)
问题描述
我是 Flask SQl 炼金术的新手;虽然我知道炼金术抽象了 sql 语法并在创建模型时使事情变得容易;有时我们希望以非常具体的方式在前端可视化数据.
I am new to Flask SQl alchemy; Though i understand that alchemy abstracts the sql syntax and makes things easy while creating models; there could be times when we want to visualize data in the front end in a very specific way.
我有以下查询,我想使用 alchemy 使用 session.query 和过滤器以及可能的分组.
I have the following query which i would like to use using alchemy using session.query and filter and possibly grouping.
我的查询内容如下:
- mysql>
SELECT status, COUNT(id) FROM bar_baz where not name = 'Foo' and not name = 'Bar' GROUP BY status
select (select COUNT(id) FROM instance where not name = 'erf' and not tiername = 'wer' and type='app') as app, (select COUNT(1) FROM instance_2 where not name= 'visq' 而不是 name = 'werf' and type='adc') as adc from dual;
我确认以下查询适用于 MySQL;我想知道我们是否有类似于
I verified that the following queries works with the MySQL; I was wondering if we have a function similar to
c = conn.cursor()
query = 'SELECT status, COUNT(id) FROM bar_baz where not name = 'Foo' and not name = 'Bar' GROUP BY status'
c.execute(query)
print c.fetchall()
class Instance(Base):
__tablename__ = 'instance'
id = Column(Integer, primary_key=True)
name = Column(String)
status = Column(String)
type = Column(String)
class Instance2(Base):
__tablename__ = 'instance_2'
id = Column(Integer, primary_key=True)
name = Column(String)
status = Column(String)
type = Column(String)
inc = Column(Integer)
兴趣查询:
select (select COUNT(id) FROM instance where not name = 'erf' and not tiername = 'wer' and type='app') as app, (select COUNT(1) FROM instance_2 where not name = 'visq' and not name = 'werf' and type='adc') as adc from dual;`
推荐答案
对于第一个查询,使用 db.func.count
生成计数表达式.从 docs 中可以看出其他所有内容.
For the first query, use db.func.count
to produce the count expression. Everything else should be obvious from the docs.
status_counts = db.session.query(BarBaz.status, db.func.count(BarBaz.id).label('count_id')
).filter(db.not_(db.or_(BarBaz.name == 'Foo', BarBaz.name == 'Bar'))
).group_by(BarBaz.status
).all()
对于第二个查询,使用 subquery()
生成可选择的查询.
For the second query, use subquery()
to produce selectable queries.
sub_app = db.session.query(db.func.count(Instance.id).label('app')
).filter(db.not_(db.or_(Instance.name == 'erf', Instance.tiername == 'wer')), Instance.type == 'app'
).subquery()
sub_adc = db.session.query(db.func.count(Instance.id).label('adc')
).filter(db.not_(db.or_(Instance2.name == 'visq', Instance2.name == 'werf')), Instance2.type == 'adc'
).subquery()
out = db.session.query(sub_app.c.app, sub_adc.c.adc).all()
这篇关于将 SQL 转换为 SQL 炼金术的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:将 SQL 转换为 SQL 炼金术
基础教程推荐
- SQL Server:只有 GROUP BY 中的最后一个条目 2021-01-01
- 将数据从 MS SQL 迁移到 PostgreSQL? 2022-01-01
- SQL Server 2016更改对象所有者 2022-01-01
- ERROR 2006 (HY000): MySQL 服务器已经消失 2021-01-01
- 在 VB.NET 中更新 SQL Server DateTime 列 2021-01-01
- 无法在 ubuntu 中启动 mysql 服务器 2021-01-01
- 如何在 SQL Server 的嵌套过程中处理事务? 2021-01-01
- 使用pyodbc“不安全"的Python多处理和数据库访问? 2022-01-01
- Sql Server 字符串到日期的转换 2021-01-01
- SQL Server 中单行 MERGE/upsert 的语法 2021-01-01