从Postgres获取数据

时间:2011-11-22 03:25:29

标签: sql postgresql

我正在使用Postgres,我有这张表:

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

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

我希望获得以下输出

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

可以使用什么查询来获取这些输出,。 感谢。

3 个答案:

答案 0 :(得分:3)

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

答案 1 :(得分:1)

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

(你没有指定表名,所以我制作了它们。)

答案 2 :(得分:1)

试试这个:

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