In SQLAlchemy, the VALUES clause is commonly used with INSERT statements to insert multiple rows. However, it is not immediately apparent within the documentation how to create a Query object that produces a similar result.
To construct a Query object equivalent to the SQL statement SELECT * FROM (VALUES (1, 2, 3)) AS sq, we can utilize the Values clause in SQLAlchemy. The resulting query will be written as follows:
from sqlalchemy import select, column, Integer from sqlalchemy.sql import Values query = select(Values(column('Number', Integer), name='sq').data([(1,), (2,), (3,)]))
While documentation on this specific usage of the VALUES clause is lacking, you can explore the test cases provided in the SQLAlchemy GitHub repository: https://github.com/sqlalchemy/sqlalchemy/blob/master/test/sql/test_values.py.
The above is the detailed content of How Can I Use SQLAlchemy's VALUES Clause to Create a SELECT Query for Multiple Rows?. For more information, please follow other related articles on the PHP Chinese website!