Praditha
Praditha

Reputation: 1172

Getting data from Postgres

I'm using Postgres, I've this table:

id | fieldname | value
----------------------
1  | price     | 10000

and

id | dyn_field | dyn_value
--------------------------
1  | bathroom  | 2
2  | bedroom   | 4

and I would like to get the following output

field     | value
---------------------
price     | 10000
bathroom  | 2
bedroom   | 4

What query can be used for get these output,.? thanks,.

Upvotes: 1

Views: 107

Answers (3)

emco
emco

Reputation: 4709

Try this:

SELECT fieldname as field, value 
FROM table1 
UNION select dyn_field as field, dyn_value as value 
FROM table2

Upvotes: 1

Adam Wenger
Adam Wenger

Reputation: 17540

SELECT fieldname AS field, value AS value
FROM tableOne
UNION ALL
SELECT dyn_field AS field, dyn_value AS value
FROM tableTwo

Upvotes: 3

Larry Lustig
Larry Lustig

Reputation: 50970

 SELECT fieldname, value FROM this_table
 UNION ALL 
 SELECT dyn_field, dyn_value FROM and_table

(You didn't specify the table names, so I made them up).

Upvotes: 1

Related Questions