Skip to content

Other operators for sub-queries (gte, eq, in, ...) #951

@tyt2y3

Description

@tyt2y3

Currently we only have in_subquery to handle subqueries. It would be nice to also have gte_subquery, or more generally an Expr::subquery to express more operators on subqueries.

let query = Query::select()
    .columns([Char::Character, Char::SizeW, Char::SizeH])
    .from(Char::Table)
    .and_where(Expr::col(Char::SizeW).in_subquery(
        Query::select()
            .expr(Expr::cust("3 + 2 * 2"))
            .take()
    ))
    .to_owned();

Corresponding PostgreSQL :

SELECT "character", "size_w", "size_h" FROM "character" WHERE "size_w" IN (SELECT 3 + 2 * 2)

Feature request

I don't think there's currently a solution to do the following :

SELECT "character", "size_w", "size_h" FROM "character" WHERE "size_w" >= (SELECT "size_w" FROM "character" WHERE "id" = "foo")

Possible solutions

let query = Query::select()
    .columns([Char::Character, Char::SizeW, Char::SizeH])
    .from(Char::Table)
    .and_where(Expr::col(Char::SizeW).gte_subquery(
        Query::select()
            .column(Char::SizeW)
            .from(Char::Table)
            .and_where(Char::Id.eq("foo"))
            .take()
    ))
    .to_owned();

let query = Query::select()
    .columns([Char::Character, Char::SizeW, Char::SizeH])
    .from(Char::Table)
    .and_where(Expr::col(Char::SizeW).gte(Expr::subquery(
        Query::select()
            .column(Char::SizeW)
            .from(Char::Table)
            .and_where(Char::Id.eq("foo"))
            .take()
    )))
    .to_owned();

Originally posted by @SteelAlloy in #673

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions