如何使用SQL组合来自多个表的数据?

时间:2011-11-09 07:11:57

标签: mysql sql join

我的MySql数据库中有以下表格:

+----------------------------------------------+
                      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时,此查询给我2行,project_id = 2

时给出4行

3 个答案:

答案 0 :(得分:2)

这应该可以解决这个问题......一个分组和一个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

EDIT修复为包含JOIN子句

答案 1 :(得分: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;

答案 2 :(得分: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