简体   繁体   English

MySQL 5.1.7有条件地联接两个表,从第二个表中按日期选择顶部记录,从第一个表中选择所有记录

[英]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_idevents.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.

相关问题 MYSQL JOIN两个表限制第二个表的日期结果 - MYSQL JOIN two tables limit results from second table by date MYSQL:两个表之间的UNION结果,如果在第二个表中找到PK,则从第一个表中删除记录 - MYSQL: UNION results between two tables where omitting records from first table if PK found in second table MYSQL从两个表中选择,但是第二个表并不总是与第一个表有关系 - MYSQL selecting from two tables but the second table not always have relationship to the first 从两个表中获取所有记录,但仅从第二个表中获取不在第一个表中的记录 - get all records from two tables but from second table only the ones that are not in first 连接表(1:n),并考虑第二个表中的两个记录 - Join tables (1:n) with considering two records from the second table mysql - 如果第一个表中有空值,如何连接两个表并仅从第二个表添加值? - mysql - how to join two tables and only add values from the second table if there are nulls in the first table? 两个表一个包含记录,第二个包含位置我想显示第一个表记录而不连接 - Two tables one contain records and second conatain location i want to show first table record with out join 比较两个表中的所有字段,并从 mysql 中的第一个表中获取不匹配的记录 - Compare all fields from two tables and get unmatched records from the first table in mysql MYSQL:查询两个表并将第二个表中的结果连接到一个数组 - MYSQL: Query two tables and join results from second table to an array 在mysql中联接两个表并从第二个表中读取值 - Join two tables in mysql and read values from the second table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM