简体   繁体   English

来自三个表的MySQL JOIN

[英]MySQL JOIN from three tables

SELECT title
     , event_date
     , event_end_date
     , individual_price
     , event_capacity
     , e.id
     , COUNT(r.event_id) registrants_count
     , COUNT(w.event_id) waiting_count 
  FROM ni803_eb_events e
  LEFT 
  JOIN ni803_eb_registrants r
    ON r.event_id = e.id
  LEFT 
  JOIN ni803_eb_waiting_lists w
    ON w.event_id = e.id
 WHERE event_capacity > 0 
 GROUP 
    BY event_date DESC;

I got three tables: 我得到了三张桌子:

  • ni803_eb_events - list of events with unique ID ni803_eb_events-具有唯一ID的事件列表
  • ni803_eb_registrants - list of people registered to events at every registrant there is event_id = ID from event list ni803_eb_registrants-每个注册者注册到事件的人员列表,其中event_id =事件列表中的ID
  • ni803_eb_waiting_lists - its just list of people who registered after event was full, same principal as registrants also there is event_id = ID of event which people registered to ni803_eb_waiting_lists-事件发生后注册的人员的完整列表,与注册人相同的主体还有event_id =人们注册到的事件的ID

I need to show ni803_eb_events where I COUNT rows from ni803_eb_registrants (id = event_id count number of same event_id) and also COUNT rows from ni803_eb_waiting_lists. 我需要显示ni803_eb_events,其中COUNT个来自ni803_eb_registrants(id = event_id计数相同event_id的数目)的行,以及COUNT个来自ni803_eb_waiting_lists的行。

When I use just one of JOINs registrants or waiting_list it work just fine.... but not both of them :( 当我仅使用JOIN注册者之一或waiting_list时,它就可以正常工作....但不是两个都:(

At screenshot below there is something wrong I marked it there are nowhere 69 registrants, there much more impossible numbers... 在下面的屏幕截图中,我将其标记为错误,那里没有69位注册者,还有更多不可能的数字...

屏幕截图-我的查询结果

The issue is because you could get a Cartesian due to not always having records in either/or table. 问题是因为您可能会得到笛卡尔坐标,这是因为并非总是在一个或多个表中都有记录。 I suggest doing the pre-aggregates grouped by ID within each sub-table. 我建议在每个子表中进行按ID分组的预聚合。 Then those are left-joined to the original table of events. 然后将这些左连接到原始事件表。 So at MOST, there would be 1 record in the registrants or 1 record in the waiting, never more. 因此,在MOST中,注册人中有1条记录,或者等待中有1条记录,再也没有。 So your outermost query does not need a COUNT() or GROUP BY clause. 因此,您最外面的查询不需要COUNT()或GROUP BY子句。 You get each event with the respective counts. 您会获得每个事件的相应计数。

SELECT 
      title, 
      event_date, 
      event_end_date, 
      individual_price, 
      event_capacity, 
      e.id, 
      reg.regCnt as registrants_count, 
      wait.waitCnt as waiting_count 
   FROM 
      ni803_eb_events e
         LEFT JOIN 
         ( select r.event_id, COUNT(*) regCnt
              from ni803_eb_registrants r
              group by r.event_id ) reg
            ON e.id = reg.event_id
         LEFT JOIN 
         ( select w.event_id, COUNT(*) waitCnt
              from ni803_eb_waiting_lists w
              group by w.event_id ) wait
            ON e.id = wait.event_id
   WHERE 
      e.event_capacity > 0 
   order by
      event_date DESC;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM