2011-11-09 79 views
1

我已經按照我的MySQL數據庫表:如何使用SQL組合來自多個表的數據?

+----------------------------------------------+ 
         PROJECT 
+----------------------------------------------| 
+----------------------------------------------| 
    project_id | team_size | from_date 
+----------------------------------------------| 
    1  | 34  | 1 Dec 2010 
+----------------------------------------------| 
    2  | 2   | 2 Jan 1902 
+----------------------------------------------| 
    3  | 99  | 15 Aug 1947 
+----------------------------------------------+ 

+----------------------------------------------+ 
     Technologies 
+----------------------------------------------+ 
+----------------------------------------------+ 
technology_id | technology_name 
+----------------------------------------------+ 
     1  | Java 
+----------------------------------------------+ 
     2  | CPP 
+----------------------------------------------+ 
     3  | Hibernate 
+----------------------------------------------+ 
     4  | EJB 
+----------------------------------------------+ 
     5  | Python 
+----------------------------------------------+ 
     6  | Hadoop 
+----------------------------------------------+ 
     7424 | Perl 
+----------------------------------------------+ 

要連結ProjectTechnologies表我有如下表:

+----------------------------------------------+ 
     Project_Technologies 
+----------------------------------------------+ 
+----------------------------------------------+ 
    Project_ID | Technology_ID 
+----------------------------------------------+ 
    1   | 2 
+----------------------------------------------+ 
    1   | 7424 
+----------------------------------------------+ 
    2   | 1 
+----------------------------------------------+ 
    2   | 3 
+----------------------------------------------+ 
    2   | 4 
+----------------------------------------------+ 
    2   | 5 
+----------------------------------------------+ 

我想顯示的數據在UI中以行和列的形式顯示在一個表中。 例如:

+---------------------------------------------------+ 
    project_id | team_size | from_date | technologies 
|---------------------------------------------------| 
|  1  | 34  | 1 Dec 2010| CPP, Perl | 
|---------------------------------------------------| 
|  2  | 2  | 2 Jan 1902| Java, Hibernate, EJB, Python | 
|---------------------------------------------------| 
|  3  | 99  |15 Aug 1947|    | 
+---------------------------------------------------+ 

我不能夠形成SQL查詢來獲取這樣的事情。我試過以下查詢,它給了我重複的行。

select pr.project_id,pr.team_size,pr.from_date,tech.technology_name 
from project pr, project_technologies ptech, technologies tl 
where pr.project_id=ptech.project_id and ptech.technology_id=tl.technology_id 

我想知道如何避免重複行?目前PROJECT_ID = 1和4行PROJECT_ID時,當此查詢是給我2行= 2

+1

你嘗試過使用DISTINCT嗎? – Muse

+0

@CodeBuzz,剛剛我嘗試了不同的,但我得到相同的輸出。我將添加有問題的當前查詢的輸出。 – Shekhar

回答

2

這應該做的伎倆......一組由固定在包括JOIN子句的GROUP_CONCAT

select 
    pr.project_id, 
    pr.team_size, 
    pr.from_date, 
    GROUP_CONCAT(tech.technology_name separator ', ') as technologies 
from 
    project pr 
    JOIN project_technologies ptech ON pr.project_id=ptech.project_id 
    JOIN technologies tl ON ptech.technology_id=tl.technology_id 
GROUP BY 
    pr.project_id, 
    pr.team_size, 
    pr.from_date 

編輯

+0

我認爲你應該在mysql中使用內部連接操作來加入兩個常用屬性表,它們具有避免重複的功能。 – vicky

+0

我加入了join的原因,原本我剛纔介紹了OP NNEEDS的修復,但是我看到修復這個問題的智慧。謝謝! –

+0

@Stefan H,真棒!!!!它的工作原理:D。非常感謝。 – Shekhar

1

嘗試這一個 -

SELECT p.*, GROUP_CONCAT(t.technology_name) technologies FROM project p 
    JOIN project_technologies pt 
    ON pt.project_id = p.project_id 
    JOIN technologies t 
    ON t.technology_id = pt.technology_id 
GROUP BY p.project_id; 
+0

您將無法選擇未包含在組中的項目,除非它們是聚合函數... –

+0

在MySQL中,它是可能的。 – Devart

+0

對不起,我應該寧願說你不應該。據我所知,它違背了ANSI sql標準,在某些mysql模式下,實際上不能返回未在組中指定的非聚合列。 –

1

使用:

 
SELECT PROJECT.*, Technologies.* FROM Project_Technologies 
INNER JOIN PROJECT ON Project_Technologies.Project_ID = PROJECT.project_id 
INNER JOIN Technologies ON Project_Technologies.Technology_ID = Technologies.technology_id 
相關問題