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
Release: 2024-12-28 14:23:10
Original
170 people have browsed it

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;
Copy after login

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,)]))
Copy after login

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!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template