Home >Database >Mysql Tutorial >Can SQLAlchemy's `VALUES` Clause Be Used in `SELECT` Queries?
Understanding the VALUES Clause in SQLAlchemy
SQLAlchemy provides versatile tools for building SQL queries. Among them, you may wonder if it's possible to execute a query equivalent to the following:
SELECT * FROM (VALUES (1, 2, 3)) AS sq;
Despite the documentation mentioning the VALUES clause only in the context of INSERT statements, SQLAlchemy now offers support for its use in SELECT queries.
Using the updated feature, your query can be expressed as:
from sqlalchemy import select, column, Integer from sqlalchemy.sql import Values query = select(Values(column('Number', Integer), name='sq').data([(1,), (2,), (3,)]))
This query essentially creates a virtual table named 'sq' from the provided data and then selects all columns from it.
You can find more information and examples in the SQLAlchemy test cases:
https://github.com/sqlalchemy/sqlalchemy/blob/master/test/sql/test_values.py
The above is the detailed content of Can SQLAlchemy's `VALUES` Clause Be Used in `SELECT` Queries?. For more information, please follow other related articles on the PHP Chinese website!