2

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

 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"
4

2 回答 2

3

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 我们可以使用listagg

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
于 2013-01-15T11:00:14.973 回答
1

这取决于您使用的 RDBMS,例如对于 MySQL,您可以将GROUP_CONCAT用于一行中的所有功能连接:

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) 中,您可以使用LISTAGG

SELECT homes.home_id,
       max(homes.title), 
       max(homes.description), 
       max(homes.living_room_count), 
       max(homes.bedroom_count), 
       max(homes.bathroom_count),
       LISTAGG(features.feature_name,',') 
          WITHIN GROUP(order by features.feature_name) as 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 10 中模拟 GROUP_CONCAT 的另一种方法

于 2013-01-15T10:53:53.057 回答