Sort by a column in a union query in SqlAlchemy SQLite

前端 未结 1 975
旧巷少年郎
旧巷少年郎 2021-01-22 16:29

As explained in this question, you can use string literals to do order by in unions.

For example, this works with Oracle:

query         


        
相关标签:
1条回答
  • 2021-01-22 17:08

    The queries that are part of a union query must not be sorted.

    To be able to use limits inside a compound query, you must wrap the individual queries inside a separate subquery:

    SELECT * FROM (SELECT ... LIMIT ...)
    UNION ALL
    SELECT * FROM (SELECT ... LIMIT ...)
    
    q1 = select(...).limit(...).subquery()
    q2 = select(...).limit(...).subquery()
    query = q1.union_all(q2)...
    
    0 讨论(0)
提交回复
热议问题