TC Fox
TC Fox

Reputation: 1030

PostgreSQL concatenate two tables on each row

I have two tables, as such (where value in all cases is a character varying):

table_one:

|id|value|
|--+-----|
|1 |A    |
|2 |B    |
|3 |C    |
|4 |D    |

table_two:

|id|value|
|--+-----|
|11|0    |
|12|1    |
|13|2    |
|14|3    |
|15|4    |
|16|5    |

I want to do a query that results in the following:

result:

|value|
|-----|
|A_0  |
|A_1  |
|A_2  |
|A_3  |
|A_4  |
|A_5  |
|B_0  |
|B_1  |
|B_2  |
|B_3  |
|B_4  |
|B_5  |
|C_0  |
|C_1  |
|C_2  |
|C_3  |
|C_4  |
|C_5  |
|D_0  |
|D_1  |
|D_2  |
|D_3  |
|D_4  |
|D_5  |

This is for a seldom run report on PostgreSQL 8.4.9, so performance isn't critical. If the answer is 9.x specific, upgrading certainly isn't out of the question, so feel free to give 9.x specific answers. Any ideas?

Upvotes: 0

Views: 730

Answers (1)

Tomas Greif
Tomas Greif

Reputation: 22653

select
 table_one.val || '_' || table_two.val
from
 table_one
 cross join table_two

Documentation for cross join.

SQLFiddle

Upvotes: 1

Related Questions