从3张桌子加入

时间:2013-06-17 15:33:53

标签: sql join

我正在尝试在视图中加入3个表;情况如下:

我有一个包含项目信息的表PROJECTS。表格结构(id主键,project_id

我有另一个表PROJECT_DESIGN,列出了项目中使用的图像。表格结构(idproject_idimage_1image_2)。

我有另一个表PROJECT_MEMBERS,列出了项目中涉及的成员。表格结构(idproject_idmember_name)。

我在PROJECT_DESIGN中有project_id=11的4个相关行。 我在PROJECT_MEMBERS中有project_id=11的2个相关行。

我想以下列方式显示数据,只有4行,如

id  project_title   image_1     image_2     member_name
11  test_1232321    118.png     s118.png    member_1
11  test_1232321    13.png           8.png      member_2
11  test_1232321    18.png           78.png     member_1
11  test_1232321    908.png     18.png          member_2

我正在使用以下查询:

SELECT pm.id, pm.project_title, dm.image_1, dm.image_2,tm.name
FROM PROJECTS AS pm
INNER JOIN PROJECT_DESIGN AS dm ON pm.id = dm.project_id
inner join PROJECT_MEMBERS as tm on pm.id = tm.project_id 
AND pm.id = 11

它给了我以下结果:4 * 2

11  test_1232321    118.png     s118.png    member_1
11  test_1232321    13.png           8.png      member_2
11  test_1232321    18.png           78.png     member_1
11  test_1232321    908.png     18.png          member_2
11  test_1232321    118.png     s118.png    member_1
11  test_1232321    13.png           8.png      member_2
11  test_1232321    18.png           78.png     member_1
11  test_1232321    908.png     18.png          member_2

请帮忙。提前致谢

2 个答案:

答案 0 :(得分:0)

尝试

SELECT pm.id, pm.project_title, dm.core_design, dm.overlay_design, dm.orientation, tm.name, tm.designation, tm.image
FROM PROJECTS AS pm
INNER JOIN PROJECT_DESIGN AS dm ON pm.id = dm.project_id
inner join PROJECT_MEMBERS as tm on pm.id = tm.project_id 
WHERE pm.id = 11
GROUP BY pm.id

并使其成为连接条件的一部分。

添加了一个小组

尝试选择不同的

SELECT DISTINCT pm.id, pm.project_title, dm.core_design, dm.overlay_design, dm.orientation, tm.name, tm.designation, tm.image
FROM PROJECTS AS pm
INNER JOIN PROJECT_DESIGN AS dm ON pm.id = dm.project_id
inner join PROJECT_MEMBERS as tm on pm.id = tm.project_id 
WHERE pm.id = 11
GROUP BY pm.id.

答案 1 :(得分:0)

基于你提供的字段,我写了这篇文章,尽管我没有清晰的表格描述,因为@ user1281385说有些测试数据会有助于澄清。

 CREATE TABLE Project_Design_INPUT AS 
 SELECT pm.id, pm.project_title, dm.image_1, dm.image_2
 FROM PROJECTS AS pm 
 INNER JOIN PROJECT_DESIGN AS dm ON pm.id = dm.project_id
 AND pm.id = 11


 SELECT pm.id, pm.project_title, pm.core_design, pm.overlay_design, pm.orientation, tm.name,  
 tm.designation, tm.image
 FROM Project_Design_INPUT AS pm
 inner join PROJECT_MEMBERS as tm on pm.id = tm.project_id 
 WHERE pm.id = 11