2013-01-15 123 views
2

嘿,我正在試圖查詢我的數據庫表中設置與表之間的表多對多關係。這裏是有問題的表的快速ERDSQL多對多查詢

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" 
+1

你會如何聚集功能,所以你可以有每個家庭一個家庭排?數它們,csv? – Jodrell

+1

您使用的是什麼RDBMS? – valex

+0

使用Oracle的Im – user1851487

回答

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

而且,這裏是another way to emulate GROUP_CONCAT in oracle 10