Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php PostgreSQL中的转置查询_Php_Postgresql_Transpose - Fatal编程技术网

Php PostgreSQL中的转置查询

Php PostgreSQL中的转置查询,php,postgresql,transpose,Php,Postgresql,Transpose,我有下表: tb_item id_item | item_name | price ---------------------------- 1 | item1 | 2000 2 | item2 | 3000 3 | item3 | 4000 及 我希望得到如下输出: item_name | price | bedroom | bathroom --------------------------------------- item1

我有下表:

tb_item
id_item | item_name | price
----------------------------
1       | item1     | 2000
2       | item2     | 3000
3       | item3     | 4000

我希望得到如下输出:

item_name | price | bedroom | bathroom
---------------------------------------
item1     | 2000  | 2       | 1
item2     | 3000  | 3       | 1
item3     | 4000  | 4       | 2
我正在使用PostgreSQL;可以使用什么查询来获取此输出?
我也在使用PHP;有一个PHP函数可以做到这一点吗?

您也可以通过几个
左JOIN
语句来实现这一点

select item_name, price, bedroom.value as bedroom, bathroom.value as bathroom
from tb_item, tb_value as bedroom, tb_value as bathroom
where bedroom.id_item=tb_item.id_item and bedroom.value_type='bedroom'
and bathroom.id_item=tb_item.id_item and bathroom.value_type='bathroom'
SELECT i.item_name
   , i.price
   , bed.value AS bedroom
   , bath.value AS bathroom
FROM tb_item AS i
LEFT JOIN tb_value AS bed ON i.id_item = bed.id_item
   AND bed.value_type = 'bedroom'
LEFT JOIN tb_value AS bath ON i.id_item = bath.id_item
   AND bath.value_type = 'bathroom'

@JonathanLeffler,我能直接使用这些功能吗?或者之前需要安装?@FrankBollack,不,这是我工作的一部分,.+1用于将
JOIN
条件与
WHERE
过滤条件分离。@a.H.是的,抱歉。我已经更新了,谢谢你的关注。非常感谢,我想投票支持你的答案,但我还不能这么做,:DThanks@Praditha如果答案对你有帮助,你也可以点击我答案旁边的复选标记来接受。
SELECT i.item_name
   , i.price
   , bed.value AS bedroom
   , bath.value AS bathroom
FROM tb_item AS i
LEFT JOIN tb_value AS bed ON i.id_item = bed.id_item
   AND bed.value_type = 'bedroom'
LEFT JOIN tb_value AS bath ON i.id_item = bath.id_item
   AND bath.value_type = 'bathroom'