我正在使用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
可以使用什么查询来获取这些输出,。 感谢。
答案 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