左联接三个表与内部联接在mysql中的两个

时间:2019-03-04 10:26:12

标签: mysql sql

我有五个桌子

tableevents

eventname eventid openingdate eventtype
alpha      2222    2019-08-07  44
beta      22299    2019-08-09  48
gama      24555    2019-06-9   47

tablesectora

eventid resultstatus liability userid  profitsectora

tablesectorb

eventid resultstatus liability userid    profitsectorb

tablesectorc

eventid resultstatus liability userid    profitsectorc

users

userid registrar_id 
io      manager
co      manager

我正在寻找的是regsitar_id假定的东西(经理)

  

如果有任何用户标识(其注册用户标识为   经理)在三个表之一,两个表或所有表tableectora,tablesectorb和   tableectorc(三个表可以具有相同的eventid,不能具有相同的eventid   eventid)2-还显示事件名称和,, eventid和开放日期

SELECT distinct tl.eventid eventid ,ul.resultstatus resultstatus,ce.eventname eventname,ce.opendate opendate,ce.eventtypeid eventtypeid ,u.registrar_id from users u, tablesectora tl left join tablesectorb uf on uf.eventid=tl.eventid left join tablesectorc ul on ul.eventid=tl.eventid left join tableevents ce on ce.eventid=ul.eventid and u.userid=tl.userid and where u.registar_id='manager'

查询正在工作,但我无法获得预期的输出

  

预期输出应为

如果只有表扇区具有数据

tablesectora

eventid resultstatus liability userid  profitsectora 
2222       complted   2000      io       20000   
  

输出

eventname eventid openingdate resultstatus  eventtype
alpha      2222    2019-08-07  completed     44

如果tableectora和b都具有数据

tablesectora

eventid resultstatus liability userid  profitsectora 
2222       complted   2000      io       20000  

tablesectorb

eventid resultstatus liability userid  profitsectorb
2222       complted   200000     io       200000 
22299      completed  555666     co       56666 
  

输出

eventname eventid openingdate resultstatus  eventtype
alpha      2222    2019-08-07  completed     44
beta       22299   2019-08-09   completed    48
  

所以您看到我只想要累积的eventid,eventname,eventtype   和,resultstatus(resultstatus对于在   每张桌子

2 个答案:

答案 0 :(得分:1)

事件在三个扇区表中的哪个发生都没有关系。因此,您可以将表与UNION ALL粘在一起,将它们视为一个表。 (对我来说,它甚至看起来像一个坏的数据模型,您应该宁可使用一个带有扇区类型列的表,而不是每个扇区一个表。)

将三个表的行粘合在一起,限制为经理,通过事件ID进行汇总并进行联接。

select e.eventname, e.eventid, e.openingdate, e.eventtype, s.resultstatus
from tableevents e
join
(
  select eventid, max(resultstatus) as resultstatus
  from
  (
    select eventid, resultstatus, userid from tablesectora
    union all
    select eventid, resultstatus, userid from tablesectorb
    union all
    select eventid, resultstatus, userid from tablesectorc
  ) glued
  where userid in (select userid from users where registrar_id = 'manager')
  group by eventid
) s on s.eventid = e.eventid
order by e.eventid;

答案 1 :(得分:0)

您应该在带有UNION语句的SUBQUERY上使用DISTINCT来实现:

SELECT DISTINCT eventname
      ,eventid 
      ,openingdate 
      ,resultstatus  
      ,eventtype
FROM (SELECT eventname
          ,A.eventid 
          ,openingdate 
          ,resultstatus  
          ,eventtype
        FROM  tableevents A
        INNER JOIN tablescorea B
        ON    A.eventid = B.eventid
        INNER JOIN users C
        ON C.userid = A.userid
      UNION SELECT DISTINCT eventname
          ,A.eventid 
          ,openingdate 
          ,resultstatus  
          ,eventtype
      FROM  tableevents A
      INNER JOIN tablescoreb B
      ON    A.eventid = B.eventid
      INNER JOIN users C
        ON C.userid = A.userid
      )