从第3表的MAX行选择最新行

时间:2013-11-14 13:11:41

标签: mysql

这是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 个答案:

答案 0 :(得分: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

答案 1 :(得分: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

答案 2 :(得分: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

答案 3 :(得分: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;