我想显示所有记录(团队ID,团队名称,实习生ID,实习生姓名,记者ID,记者姓名)。我不知道如何将同一列中的实习生与另一列中的记者分开。我之所以要这样做,是因为我希望每个记者都能为他们在该小组工作的每个实习生提供评估。
答案 0 :(得分:0)
您可以对Intern和Reporter使用带有子查询的联接
select a.teamID, a.temaName,
b.peopleID intren_id, b.firstname intern_name, b.lastname inter_lastname,
c.peopleId, c.firstname reporter_name, c.lastname reporter_lastname
from mytable a
inner join (
select teamId, firstname, lastname
from mytable
where pType = 'Intern'
) b on a.teamId = b.teamId
inner join (
select teamId, firstname, lastname
from mytable
where pType = 'Reporter'
) c on a.teamId = c.teamId
答案 1 :(得分:0)
我认为您想要垂直的“列表”列。这不是真正的关系结构,因为行中的值彼此不相关。
但是,您可以使用条件聚合来实现。但是,您需要枚举值。在MySQL 8+中,使用row_number()
很容易,因此查询如下:
select teamid, teamname,
max(case when ptype = 'intern' then peopleid end) as intern,
max(case when ptype = 'reporter' then peopleid end) as reporter
from (select t.*,
row_number() over (partition by teamid, ptype order personid) as seqnum
from t
) t
group by teamid, teamname, seqnum;