Is there a generic workaround to express a derived column list in Oracle (and MySQL)?

后端 未结 3 1404
时光说笑
时光说笑 2021-02-19 00:20

Many SQL databases support what the SQL standard calls a . Such databases include at least CUBRID, Derby, Firebird, HSQLDB, Postgres, SQL

3条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-02-19 00:48

    For a MySQL solution, you could use a UNION to set the names of all the columns in a zero-row query term, and then subsequently query something more complex:

    SELECT null AS a, null AS b, null AS c FROM dual WHERE false
    UNION ALL
    SELECT , , 
    FROM ...
    

    Only the first query term of a UNION defines the column names of the whole query. Column names (or lack thereof) in subsequent query terms don't affect the ultimate column names.

    You do need to know the number of columns, but it should be pretty easy to keep the two query terms separate. As far as I know, it works in both Oracle and MySQL (however, I have only tested it in MySQL, not in Oracle).

提交回复
热议问题