[英]MySQL 5.1.7 conditional join of two tables selecting top record by date from second table and all records from first
I have two tables: 我有两个表:
instructions
— one unique record for each person instructions
-每个人的唯一记录 events
— multiple records for each person; events
-每个人有多个记录; records are of various types 记录是各种类型的 I need to retrieve the compete list of instructions and, for each one, the 'appointment'
event with the greatest (latest) date. 我需要检索竞争性指令列表,以及每个指令的日期(最大)的'appointment'
事件。
I've experimented with group by
, but haven't managed to get the right results. 我已经对group by
试验,但没有获得正确的结果。
The instructions
table: instructions
表:
id first_name surname telephone
---- ------------ --------- -----------
1 bob marley 555-1234
2 steve pike 555-3456
3 daniel osborne 555-9876
4 mark hodge 555-6600
5 stefan belfant 555-8080
The events
table: events
表:
id instruction_id type commment event_date
----- ---------------- ------------ --------- ------------
1 1 create na 2013-11-18
2 2 call na 2013-11-19
3 2 appointment onsite 2013-12-02
4 3 create na 2013-09-17
5 3 appointment office 2013-09-17
6 3 finalize as discuss 2013-11-19
7 4 create na 2013-12-02
8 4 cancel na 2013-12-02
9 5 create na 2013-10-02
10 1 appointment at home 2013-11-22
11 2 appointment at home 2013-12-05
The desired output: 所需的输出:
id first_name surname telephone appointment comment
---- ------------ --------- ----------- ------------- ---------
1 bob marley 555-1234 2013-11-22 at home
2 steve pike 555-3456 2013-12-05 at home
3 daniel osborne 555-9876 2013-09-17 office
4 mark hodge 555-6600
5 stefan belfant 555-6060
Assuming that the latest/greatest appointment-date for any given person is unique, you can write: 假设任何给定人员的最新/最晚约会日期都是唯一的,则可以这样写:
SELECT instructions.*,
latest_events.event_date AS appointment,
latest_events.comment
FROM instructions
LEFT
OUTER
JOIN ( SELECT events.instruction_id,
MAX(events.event_date) AS latest_event_date
FROM events
WHERE events.type = 'appointment'
GROUP
BY events.instruction_id
) latest_event_dates
ON instructions.id = latest_event_dates.instruction_id
LEFT
OUTER
JOIN events latest_events
ON latest_event_dates.instruction_id = latest_events.instruction_id
AND latest_event_dates.latest_event_date = latest_events.event_date
AND 'appointment' = latest_events.type
;
(If the latest/greatest appointment-date is not unique, then you'll need to add another step in the join where you select the MIN(events.id)
or MAX(events.id)
or whatnot for a given events.instruction_id
and events.event_date
.) (如果最晚/最晚的约会日期不是唯一的,那么您需要在MIN(events.id)
添加另一步骤,在该步骤中,您可以为给定的events.instruction_id
选择MIN(events.id)
或MAX(events.id)
或events.instruction_id
和events.event_date
。)
Try this 尝试这个
SELECT I.id,
I.first_name,
I.surname,
I.telephone,
E.event_date,
E.comment
FROM instructions I
LEFT JOIN
(SELECT *
FROM
(SELECT *
FROM EVENTS
WHERE EVENTS.type='appointment'
ORDER BY EVENTS.event_date DESC) EVENTS
GROUP BY EVENTS.type,
EVENTS.instruction_id ) E ON I.id=E.instruction_id
Check Sql Fiddle 检查SQL小提琴
In the end the solution came from one of the staff working for me.... It's lightening quick and exactly what is required: 最后,解决方案来自为我工作的一名员工。...它迅速而准确地满足了要求:
SELECT i.id, ins.type, ins.event_date FROM instructions i left join instructions i on ins.id = (select inss.id FROM events inss WHERE inss.instruction_id = i.id AND inss.type = 'Appointment' ORDER BY inss.event_date DESC LIMIT 1 ); 选择i.id,ins.type,ins.event_date FROM指令,我在ins.id =上离开联接指令i(从insss中选择inss.id,而inss.instruction_id = i.id AND inss.type ='约会'排序inss.event_date DESC LIMIT 1);
The Group By method, no matter how I chose to do it, always returned odd results; 无论我选择如何使用Group By方法,总是返回奇怪的结果;
thank you everyone for your help on this, greatly appreciated. 谢谢大家在此方面的帮助,不胜感激。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.