0

这是与this相关的查询,但它涉及 3 个表而不是 2 个。

我们有 3 张桌子:


People:
personid, name

PeopleEvents:
personid,eventid,eventdate

EventTypes:
eventid,eventname,eventsequence

这些表的数据如下:


People:
1,John
2,Mary
3,Jane
4,Rob

PeopleEvents:
1,100,1/1/2013
2,100,2/2/2013
2,102,2/3/2013
2,104,3/3/2013
3,100,4/4/2013
3,102,4/4/2013
4.100,2/2/2013
4,103,4/4/2013
4,104,3/3/2013

EventTypes:
100,Joined company,10
101,Induction,20
102,Introduction,17
103,Second meeting,25
104,First meeting,15

我们希望能够根据最高 EventType 序列而不是事件 ID 提取人员列表。

因此,报告将是:


John,Joined Company
Mary,Introduction
Jane,Introduction
Rob,Second meeting

这对我来说似乎不优雅但似乎有效:


SELECT p.personid,et.eventname FROM people p 
INNER JOIN peopleevents pe ON p.personid = pe.personid
INNER JOIN eventtypes et ON et.eventid = pe.eventid
WHERE p.personid NOT IN
(SELECT p1.personid FROM people p1 LEFT JOIN peopleevents pe1 ON p1.personid = p.personid
LEFT JOIN eventtypes et1 ON et1.eventid = pe1.eventid
WHERE p1.personid = p.personid AND et1.eventseq > et.eventset) ORDER BY p.name;

我看到它的方式是子查询试图找到比主查询更高的序列,因此只允许主查询返回最高序列的事件。

我将输入提供的答案,看看其中一个是否会提供更整洁的解决方案。

4

4 回答 4

1

这应该给你你的答案

编辑我正在努力解决我得到的结果,因为您的示例数据在期望的结果中是错误的,玛丽应该是“第一次会议”

编辑 2因此,正如您所说,序列是表 eventTypes 上的第三个字段(我以为是第一个字段)

select p.name, e.eventname 
from People p,
     EventTypes e,
(select p.id, max(e.seq) seq
  from People p,
       PeopleEvents pe,
       EventTypes e
 where p.id = pe.pid
   and e.id = pe.eid
 group by p.id) pe
where p.id = pe.id
  and e.seq = pe.seq

在新的小提琴链接上看到它:http ://sqlfiddle.com/#!2/1183c/1

于 2013-11-14T13:29:57.540 回答
1

试试这个查询 -

SELECT * FROM (
  SELECT p.*, et.eventname, et.eventsequence FROM people p
    JOIN PeopleEvents pe
      ON p.personid = pe.personid
    JOIN EventTypes et
      ON et.eventid = pe.eventid
  ORDER BY eventsequence DESC
) t
GROUP BY personid
于 2013-11-14T13:30:19.327 回答
1
select p.name, group_concat(e.eventname)
from People p
join (p1.personid, max(et1.eventsequence)  as eventsequence
      from People p1
      join PeopleEvents pe1 on p1.personid = pe1.personid
      join EventTypes et1 on pe1.eventid = et1.eventid
      ) sub
  on p.personid = sub.personid
join PeopleEvents pe on p.personid = pe.personid 
join EventTypes et on pe.eventid = et.eventid
  and et.eventsequence = sub.eventsequence
group by p.personid

这将为您提供具有最高事件序列的所有人的列表。如果他们以相同的顺序参加了两个活动并且他们是最后一个,则两个活动都会出现。即使有超过 1 个同名的人,每个人也会得到一个单独的行。

更新让草莓开心。结果是一样的,顺便说一句:

SELECT p.name, GROUP_CONCAT(e.eventname)
FROM People p
JOIN PeopleEvents pe ON p.personid = pe.personid 
JOIN EventTypes et ON pe.eventid = et.eventid
WHERE et.eventsequence = (SELECT MAX(et1.eventsequence)
                          FROM PeopleEvents pe1
                          JOIN EventTypes et1 ON pe1.eventid = et1.eventid
                          WHERE pe1.personid = p.personid)
GROUP BY p.personid
于 2013-11-14T13:40:40.990 回答
1

好的。我会这样做。我已经根据我自己的命名约定重命名了您的几个专栏——这是从“笔岛”大灾难中产生的一种习惯。

SELECT p.name
     , et.event_name
  FROM people p
  JOIN people_events pe 
    ON p.person_id = pe.person_id 
  JOIN event_types et 
    ON et.event_id = pe.event_id 
  JOIN  
     ( SELECT pe.person_id
            , MAX(et.event_sequence) max_event_sequence
         FROM people_events pe
         JOIN event_types et 
           ON et.event_id = pe.event_id   
        GROUP 
           BY pe.person_id
     ) y
    ON y.person_id = p.person_id
   AND y.max_event_sequence = et.event_sequence;
于 2013-11-14T14:15:34.270 回答