首页 > 解决方案 > SQLAlchemy 加入函数 - 如何引用返回的列?

问题描述

我有一个要使用 sqlalchemy 重新创建的查询

SELECT * FROM some_table AS st
INNER JOIN MY_FUNCTION(123, 456) mf ON st.id = mf.id
INNER JOIN other_table AS ot ON st.other_id = ot.other_id

whereMY_FUNCTION是一个返回临时表的函数。可能是由于不知道所有术语,我很难制定出 sqlalchemy 语法来重新创建这个 sql - 我知道我可以select_from()为连接做一个,但我不确定如何使用该函数重新创建连接。

这是在 postgresql 中,但理想情况下也适用于 sqlserver。在我的脑海中,我想象它是这样的:

some_table = Table("some_table", ...)
my_function = func.my_function(123, 456)
other_table = Table("other_table", ...)

some_table.select_from(
    some_table.join(
        my_function,
        some_table.c.id == my_function.c.id  # myfunction.c.id attribute error
    ).join(
        other_table,
        some_table.c.other_id == other_table.c.other_id
    )
)

编辑:我已经取得了进展,我相信 sqlalchemyfunc将返回函数对象,但我不确定如何引用将在join()

标签: sqlsql-serverpostgresqlsqlalchemy

解决方案


推荐阅读