SQL syntax term for 'WHERE (col1, col2) < (val1, val2)'

前端 未结 3 788
梦谈多话
梦谈多话 2020-11-30 11:50

As my question states, I would like to know what we call types of queries with that type of condition in the WHERE clause, i.e.:

SELECT * FROM m         


        
相关标签:
3条回答
  • 2020-11-30 12:29

    Expression Lists

    Use of Row Value Constructors (RVC) in Comparison Predicates - quite a long term

    RVCs are usually seen in INSERT statements, but rarely as part of WHERE clauses.

    I doubt that this syntax has a direct support in JPA or Hibernate Criteria API, but there is always a workaround to achieve the same logic.

    0 讨论(0)
  • 2020-11-30 12:45

    The common short term is just "Row values". Or "Row value comparison" for the operation you demonstrate. That feature has been in the SQL standard since SQL-92 (!). Postgres is currently the only major RDBMS that supports it in all aspects - especially also with optimal index support.

    In particular, the expression (col1, col2) < (1, 2) is just shorthand for ROW(col1, col2) < ROW(1, 2) in Postgres. The expression ROW(col1, col2) is also called row constructor - just like ARRAY[col1, col2] is an array constructor.

    It is conveniently short for the more verbose, equivalent expression:

    col1 < 1 OR (col1 = 1 AND col2 < 2)
    

    ... and Postgres can use an index on (col1, col2) or (col1 DESC, col2 DESC) for this.

    And notably distinct from (!)

    col1 < 1 AND  AND col2 < 2
    

    Consider example: (1,1) ...

    Here is a presentation by Markus Winand that discusses the feature for pagination in detail:

    "Pagination done the PostgreSQL way" on use-the-index-luke.com.

    Row value comparison starts on page 20. The support matrix I have been referring to is on page 45.

    I am in no way affiliated to any of the sources I quoted.

    0 讨论(0)
  • 2020-11-30 12:45
    WHERE (col1, col2) < (val1, val2)
    

    Above syntax is called Row Value Constructor/tuple syntax/Row Subquery.

    From doc

    ANSI SQL row value constructor syntax, sometimes referred to AS tuple syntax, even though the underlying database may not support that notion. Here, we are generally referring to multi-valued comparisons, typically associated with components

    Alternatively it can be called Row Subqueries

    0 讨论(0)
提交回复
热议问题