SQL多对多查询

SQL多对多查询,sql,oracle,join,many-to-many,Sql,Oracle,Join,Many To Many,嘿,我正在尝试查询我的数据库表,这些表是在多对多关系中设置的,它们之间有一个表。这里是一个有问题的表格的快速erd Homes ----< Home_Feature >---- Features 查询的输出: Title Feature .... 1 House A Balcony 2 House A Pool 3 House A Garage 4 House B Air-Con 谢谢你,谢谢你的帮助 ____

嘿,我正在尝试查询我的数据库表,这些表是在多对多关系中设置的,它们之间有一个表。这里是一个有问题的表格的快速erd

 Homes ----<  Home_Feature  >---- Features
查询的输出:

    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"

这取决于您使用的RDBMS,例如MySQL,您可以在一行中用于所有功能的串联:

SELECT homes.home_id,
       max(homes.title), 
       max(homes.description), 
       max(homes.living_room_count), 
       max(homes.bedroom_count), 
       max(homes.bathroom_count),
       GROUP_CONCAT(features.feature_name) features
 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
 GROUP_BY homes.home_id
在ORACLE 11g r2中,您可以使用


这里还有一个

它取决于您使用的RDBMS,例如MySQL,您可以在一行中将所有功能串联起来:

SELECT homes.home_id,
       max(homes.title), 
       max(homes.description), 
       max(homes.living_room_count), 
       max(homes.bedroom_count), 
       max(homes.bathroom_count),
       GROUP_CONCAT(features.feature_name) features
 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
 GROUP_BY homes.home_id
在ORACLE 11g r2中,您可以使用

这里还有Oracle 10g版本:

SQL> SELECT homes.title, homes.living_room_count, homes.bedroom_count, homes.bathroom_count,
  2         wm_concat(features.feature_name) features
  3    FROM homes
  4         INNER JOIN home_feature
  5                 ON homes.home_id = home_feature.home_id
  6         INNER JOIN features
  7                 ON home_feature.feature_id = features.feature_id
  8   group by homes.title, homes.living_room_count, homes.bedroom_count, homes.bathroom_count;

TITLE   LIVING_ROOM_COUNT BEDROOM_COUNT BATHROOM_COUNT FEATURES
------- ----------------- ------------- -------------- ------------------------------
House A                 1             3              1 Balcony,Pool,Garage
House B                 1             2              2 Air-Con
和11g,我们可以使用:

Oracle 10g版本:

SQL> SELECT homes.title, homes.living_room_count, homes.bedroom_count, homes.bathroom_count,
  2         wm_concat(features.feature_name) features
  3    FROM homes
  4         INNER JOIN home_feature
  5                 ON homes.home_id = home_feature.home_id
  6         INNER JOIN features
  7                 ON home_feature.feature_id = features.feature_id
  8   group by homes.title, homes.living_room_count, homes.bedroom_count, homes.bathroom_count;

TITLE   LIVING_ROOM_COUNT BEDROOM_COUNT BATHROOM_COUNT FEATURES
------- ----------------- ------------- -------------- ------------------------------
House A                 1             3              1 Balcony,Pool,Garage
House B                 1             2              2 Air-Con
和11g,我们可以使用:


您将如何聚合这些功能,以便每个家庭有一个主行?计算它们,csv?您能提供示例表和所需输出吗?您如何聚合功能,以便每个家庭有一个主行?计算它们,csv?您能提供示例表和所需输出吗??
SQL> SELECT homes.title, homes.living_room_count, homes.bedroom_count, homes.bathroom_count,
  2         listagg(features.feature_name, ',') within group (order by features.feature_name)  features
  3    FROM homes
  4         INNER JOIN home_feature
  5                 ON homes.home_id = home_feature.home_id
  6         INNER JOIN features
  7                 ON home_feature.feature_id = features.feature_id
  8   group by homes.title, homes.living_room_count, homes.bedroom_count, homes.bathroom_count;

TITLE   LIVING_ROOM_COUNT BEDROOM_COUNT BATHROOM_COUNT FEATURES
------- ----------------- ------------- -------------- ------------------------------
House A                 1             3              1 Balcony,Garage,Pool
House B                 1             2              2 Air-Con