简体   繁体   English

python sqlalchemy结合两个表的查询

[英]python sqlalchemy combine query from two tables

I have two tables that are not related like this: 我有两个不相关的表:

hurst = Table('Hurst', metadata,
    Column('symbol_id' , Integer, primary_key=True), 
    Column('Date'      , String(20), nullable=False),
    Column('symbol'    , String(40), nullable=False),
    Column('HurstExp'  , Float,      nullable=False),
)

fundamental = Table('Fundamental', metadata,
    Column('symbol_id' , Integer,    primary_key=True), 
    Column('Date'      , String(20), nullable=False),
    Column('symbol'    , String(40), nullable=False),
    Column('MarketCap' , Float,      nullable=False),
)

Each of the following queries works fine. 以下每个查询都可以正常工作。 How do I combine them so I can get only the hurst of companies that are worth more than 50,000,000,000? 如何将它们结合起来,所以我可以得到只有那些价值超过500亿以上公司的赫斯特?

# -*- coding: utf-8 -*-
"""
Created on Sun Dec 13 19:22:35 2015

@author: idf
"""

from sqlalchemy import *

def run(stmt):
    rs = stmt.execute()
    return rs

# Let's re-use the same database as before
dbh = create_engine('sqlite:///hurst.db')
dbf = create_engine('sqlite:///fundamental.db')

dbh.echo = True  # We want to see the SQL we're creating
dbf.echo = True  # We want to see the SQL we're creating

metadatah = MetaData(dbh)
metadataf = MetaData(dbf)

# The users table already exists, so no need to redefine it. Just
# load it from the database using the "autoload" feature.
hurst = Table('Hurst',       metadatah, autoload=True)
funda = Table('Fundamental', metadataf, autoload=True)

hurstQ = hurst.select(hurst.c.HurstExp < .5)
run(hurstQ)

fundaQ = funda.select(funda.c.MarketCap > 50000000000)
run(fundaQ)

If I try to use a join, I get an error: 如果我尝试使用连接,我会收到错误:

j = join(hurst, funda, hurst.c.symbol == funda.c.symbol)
stmt = select([hurst]).select_from(j)
theJoin = run(stmt)


Traceback (most recent call last):
  File "/home/idf/anaconda3/lib/python3.5/site-packages/sqlalchemy/engine/base.py", line 1139, in _execute_context
    context)
  File "/home/idf/anaconda3/lib/python3.5/site-packages/sqlalchemy/engine/default.py", line 450, in do_execute
    cursor.execute(statement, parameters)

   cursor.execute(statement, parameters)
sqlite3.OperationalError: no such table: Fundamental

I can't even do the easy version 我甚至不能做简单的版本

# This will return more results than you are probably expecting.
s = select([hurst, funda])
run(s)

Don't have an environment to test this in at the moment, but something along the lines of this should work: 目前没有环境可以对此进行测试,但是应该采取以下措施:

j = join(husrt, funda, (hurst.c.symbol_id == funda.c.symbol_id) & (funda.c.MarketCap > 50000000000))
stmt = select([hurst]).select_from(j)
run(stmt)

Check out sqlalchemy.sql.expression.join on this SQL Alchemy docs page. 这个SQL Alchemy文档页面上查看sqlalchemy.sql.expression.join

I don't believe you can join between two databases . 我不相信你可以加入两个数据库

One of your tables is in one database and the other is in another database: 您的一个表位于一个数据库中,另一个位于另一个数据库中:

dbh = create_engine('sqlite:///hurst.db')
dbf = create_engine('sqlite:///fundamental.db')

You should put all tables in the same database.db file and have one db = create_engine('sqlite:///database.db') . 您应该将所有表放在同一个database.db文件中,并使用一个db = create_engine('sqlite:///database.db')

Correction: You can do that: Cross database join in sqlalchemy 更正:您可以这样做: 在sqlalchemy中交叉数据库加入

But do you really want to have each table in a separate database? 但是你真的想将每个表放在一个单独的数据库中吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM