sql - 加入2个表10次

时间:2012-04-19 10:34:21

标签: mysql sql database sqlite

我有2个像这样的结构表:

People
----------
>ID, Name, Surname

Projects
----------
>ID, Assistant#1,  Assistant#2,  Assistant#3,  Assistant#4, 
> Assistant#5,  Assistant#6,  Assistant#7,  Assistant#8,  Assistant#9, 
> Assistant#10, lot of other columns..... (every assistant column
> contains an integer, the people ID)

现在,我想查询我的数据库并从Project表中获取一行(让我们假设ID = 3的行)包含项目表的所有列,特别是每个助手都用它的正确名称和姓氏替换。 我已经设法做了这个(有很多连接),但是凭借我的小经验,我的代码看起来非常笨重,而且我确信有更好的方法可以做到这一点。

提前致谢。

8 个答案:

答案 0 :(得分:6)

SELECT p.Name, p.Surname
FROM People p
CROSS JOIN Project j ON (p.PeopleID = j.Assistant1 OR
p.PeopleID = j.Assistant2 OR
p.PeopleID = j.Assistant3 OR
.. etc)
AND j.ProjectID = 3

你的表现会很糟糕,但这是你为拥有一个设计糟糕的数据库而付出的代价。更好的解决方案是通过分解来映射项目和人员:

CREATE TABLE proj_people
People_ID INT
Project_ID INT

答案 1 :(得分:4)

我的结构不清楚。我建议你为项目创造一个离合器。

表:

Peoples people_id,people_name,people_surname

Projects project_id,project_name

Clutch project_id,people_id

答案 2 :(得分:0)

未经测试:)

Select * from project pr
left join people pe1 on pe1.peopleid = pr.astint1
left join people pe2 on pe2.peopleid = pr.astint2
left join people pe3 on pe3.peopleid = pr.astint3
left join people pe4 on pe4.peopleid = pr.astint4
left join people pe5 on pe5.peopleid = pr.astint5 
left join people pe6 on pe6.peopleid = pr.astint6
left join people pe7 on pe7.peopleid = pr.astint7
left join people pe8 on pe8.peopleid = pr.astint8
left join people pe9 on pe9.peopleid = pr.astint9
left join people pe10 on pe10.peopleid = pr.astint10
where pr.projectid = 3

答案 3 :(得分:0)

也许更清洁的设计是:

人员表:

People ID, Name, Surname

ProjectAssistants表:

Project ID, Assistant Person ID

项目表:

Project ID, lots of other columns (but no Assistant columns)

然后,SQL就变成了:

SELECT pr.*, p.* 
FROM Projects pr 
JOIN ProjectAssistants p_a ON pr.ProjectID = p_a ProjectID 
JOIN People p ON p_a.AssistantPersonID = p.PeopleID 
WHERE ProjectID = X

答案 4 :(得分:0)

您的问题是您正在尝试在数据库级别解决数据呈现问题,这很少是一个好主意。你应该有一个只包含projectid和assistantid的表,将两列的组合作为表的主键。这将允许您只将此表连接到助手表和项目表一次,并获得所需的所有结果。这样做的好处是,您可以在将来支持更多项目助理,而无需更改查询以添加更多联接。

总之,你应该有3个表:

Project
Assistant
ProjectAssistant (projectid,assistantid)

答案 5 :(得分:0)

您要重组表格。我讨厌多次查看具有多列的表格。如果重组为3个表:

PeopleID | Name | Surname

项目

ProjectID | ...other columns except Assistant

Project_Person

ProjectID | PersonID

这样,您可以将多个助手分配到同一个项目,而无需多个助手列。然后你可以使用这样的东西来获得所需的结果:

SELECT proj.ProjectID, pers.Name, pers.Surname
FROM Person pers
INNER JOIN Project_Person pp ON pp.PersonID = pers.PersonID
INNER JOIN Project proj ON proj.ProjectID = pp.ProjectID

这不会返回单行,但这不是SQL的工作方式。

答案 6 :(得分:0)

如果您不想/不能更改表结构,那么可能使用用户定义的函数?

CREATE FUNCTION GET_ASS_NAME (n1 ASS_NO) RETURNS VARCHAR(50) DETERMINISTIC BEGIN DECLARE fullname VARCHAR(50); SELECT fullname = CONCAT(NAME, ' ', SURNAME) FROM PEOPLE WHERE ID = n1; RETURN fullname; END|

然后

select GET_ASS_NAME(1), GET_ASS_NAME(2), GET_ASS_NAME(3), ... from PROJECTS where ID = 3

答案 7 :(得分:0)

因为您将一些(可能的)助手设置为10,这就要求您必须编写代码来容纳这10名助手。这可以通过以下几种方式完成:(尚未测试)

select * from projects proj
left join people p1 on proj.assistant1 = p1.peopleid
left join people p2 on proj.assistant2 = p2.peopleid
left join people p3 on proj.assistant3 = p3.peopleid
left join people p4 on proj.assistant4 = p4.peopleid
left join people p5 on proj.assistant5 = p5.peopleid
left join people p6 on proj.assistant6 = p6.peopleid
left join people p7 on proj.assistant7 = p7.peopleid
left join people p8 on proj.assistant8 = p8.peopleid
left join people p9 on proj.assistant9 = p9.peopleid
left join people p10 on proj.assistant10 = p10.peopleid

否则你可以用

做一些诡计
select proj.projectID, 
(select * from people where peopleID = proj.assistant1),
(select * from people where peopleID = proj.assistant2),
(select * from people where peopleID = proj.assistant3),
(select * from people where peopleID = proj.assistant4),
(select * from people where peopleID = proj.assistant5),
(select * from people where peopleID = proj.assistant6),
(select * from people where peopleID = proj.assistant7),
(select * from people where peopleID = proj.assistant8),
(select * from people where peopleID = proj.assistant9),
(select * from people where peopleID = proj.assistant10)
from projects proj

如果可能,最好重新构建数据表,并将单个助手映射到单个projectID:

PeopleID, Name, Surname

ProjectID, PeopleID

因此,您只能进行一次内连接,并为每位助手返回一行:

select * from projects proj 
inner join people p 
on p.peopleID = proj.peopleid
where proj.projectID = PROJECTID