Many SQL databases support what the SQL standard calls a
. Such databases include at least CUBRID, Derby, Firebird, HSQLDB, Postgres, SQL
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).