0

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

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

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

我有另一个表格PROJECT_MEMBERS,列出了参与项目的成员。表结构(id, project_id, member_name)。

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

我想以下列方式显示数据,只有 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

请帮忙。提前致谢

4

2 回答 2

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  
于 2013-06-17T16:01:50.627 回答
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.
于 2013-06-17T15:38:14.363 回答