Home >Database >Mysql Tutorial >How Can SQLAlchemy's VALUES Clause Be Used to Build SELECT Queries?

How Can SQLAlchemy's VALUES Clause Be Used to Build SELECT Queries?

DDD
DDDOriginal
2024-12-28 14:23:10207browse

How Can SQLAlchemy's VALUES Clause Be Used to Build SELECT Queries?

Building Queries with VALUES Clause in SQLAlchemy

In SQLAlchemy, the VALUES clause is not limited to INSERT statements as the documentation suggests. It has now been expanded to enable the creation of Query objects that mimic the functionality of the SQL query:

SELECT * FROM (VALUES (1, 2, 3)) AS sq;

To achieve this in SQLAlchemy, use the following syntax:

from sqlalchemy import select, column, Integer
from sqlalchemy.sql import Values

query = select(Values(column('Number', Integer), name='sq').data([(1,), (2,), (3,)]))

Despite its relative obscurity, this feature is evident in the test cases located at https://github.com/sqlalchemy/sqlalchemy/blob/master/test/sql/test_values.py.

The above is the detailed content of How Can SQLAlchemy's VALUES Clause Be Used to Build SELECT Queries?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn