嘿,我正在尝试查询我的数据库表,这些表设置在与表之间的多对多关系中。这是有问题的表格的快速查询
Homes ----< Home_Feature >---- Features
我已经尝试创建下面的 sql 查询,但是有没有办法为每个家庭返回一行而不是这里返回的许多?或者我是否必须更改我的表结构以适应更好的解决方案?
SELECT homes.title, homes.description, homes.living_room_count, homes.bedroom_count, homes.bathroom_count, features.feature_name
FROM homes
INNER JOIN home_feature
ON homes.home_id = home_feature.home_id
INNER JOIN features
ON home_feature.feature_id = features.feature_id;
查询的输出:
Title Feature ....
1 House A Balcony
2 House A Pool
3 House A Garage
4 House B Air-Con
谢谢,任何帮助表示赞赏!
____________________EDIT__________________________
嘿,我非常感谢你们到目前为止所提供的帮助,并且想知道我是否可以在添加到此查询和从另一个表中选择列方面获得更多帮助。
当我只是在 SELECT 语句中添加另一个表的列和 FROM 子句中的表时,查询似乎不起作用?我使用的查询如下,但不起作用。再次感谢您的帮助。
SELECT homes.title, homes.description, homes.living_room_count, homes.bedroom_count, homes.bathroom_count, homes.price, homes.sqft, home_type.type_name,
listagg(features.feature_name, ',') WITHIN GROUP (ORDER BY features.feature_name) features
FROM homes, home_type
INNER JOIN home_feature
ON homes.home_id = home_feature.home_id
INNER JOIN features
ON home_feature.feature_id = features.feature_id
GROUP BY homes.title, homes.description, homes.living_room_count, homes.bedroom_count, homes.bathroom_count, homes.price, homes.sqft;
我收到此错误:
ORA-00904: "HOMES"."HOME_ID": invalid identifier
00904. 00000 - "%s: invalid identifier"