我已經按照我的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
+----------------------------------------------+
要連結Project
和Technologies
表我有如下表:
+----------------------------------------------+
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
你嘗試過使用DISTINCT嗎? – Muse
@CodeBuzz,剛剛我嘗試了不同的,但我得到相同的輸出。我將添加有問題的當前查詢的輸出。 – Shekhar