7

Possible Duplicate:
Can you define “literal” tables in SQL?

Occasionally I find myself in a situation where I'd like to join an existing table to a table of values that are entered in the query. Something like:

SELECT ((1,2,3),(4,5,6)); 

Where the query would return two rows of 3 columns. Obviously this syntax is not correct, but it is possible to generate a single row of data in this way. For example:

SELECT 1,2,3;

Is there actually a way to do what I'm trying to achieve?

Community
  • 1
  • 1
andrewmabbott
  • 779
  • 6
  • 8

1 Answers1

10
SELECT 1,2,3
UNION ALL
SELECT 4,5,6;
gbn
  • 422,506
  • 82
  • 585
  • 676
  • Brilliant, thanks. I was hoping it would be more elegant but it works! I can use it combination with this http://stackoverflow.com/questions/1764881/mysql-getting-data-for-histogram-plot to generate histogram data without needing to create an extra table. – andrewmabbott Oct 28 '11 at 10:58
  • is there a way to name the columns in this, without specifying the same "AS column1"... in each line? – Ubeogesh Apr 05 '21 at 13:45
  • 1
    yes just give names to the first select : select 2 as id, 3 as code union all select 1, 4 – Romain Hautefeuille Aug 11 '21 at 08:22