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!