MySQL-SUM,COUNT和JOIN显示所有记录

时间:2018-07-25 10:59:22

标签: mysql

这是对此Join two tables with SUM and COUNT的一个后续问题。 我想做的是使所有值都显示在历史记录表中,而不是在旋转表中,反之亦然(999和777)

所以我的表是:

create table history (
    code int(10) primary key,
    PN varchar(10) not null,
    Qty int(10) not null,
    LOC_ID int(10));

insert into history values (1,  'T1', 1, 1);
insert into history values (2,  'A1', 2,2);
insert into history values (3,  'J1', 3,3);
insert into history values (4,  'A2', 1,4);
insert into history values (5,  'J2', 2,1);
insert into history values (6,  'A3', 3,2);
insert into history values (7,  'J3', 4,3);
insert into history values (8,  'T1', 5,4);
insert into history values (9,  'A1', 1,1);
insert into history values (10, '999', 3,2);
insert into history values (11, 'J2', 4,3);
insert into history values (12, 'A1', 3,4);
insert into history values (13, 'J2', 5,1);

create table rota (
        code int(10) primary key,
    PN varchar(10) not null,
    SN varchar(10) not null,
    LOC_ID int(10));

insert into rota values (1,     'T1',   't1a',1);
insert into rota values (2,     'A1',   'a1a',2);
insert into rota values (3,     'J1',   'j1a',3);
insert into rota values (4,     'A2',   'a2a',4);
insert into rota values (5,     'J2',   'j2a',1);
insert into rota values (6,     'A3',   'a3a',2);
insert into rota values (7,     'J3',   'j3a',3);
insert into rota values (8,     '777',   't1b',4);
insert into rota values (9,     'A1',   'a1b',1);
insert into rota values (10,    'J2',   'j2b',2);
insert into rota values (11,    'J2',   'j2c',3);
insert into rota values (12,    'A1',   'a1c',4);
insert into rota values (13,    'J2',   'j2d',1);
insert into rota values (14,    'J2',   'j2e',2);
insert into rota values (15,    'J2',   'j2f',3);

create table loca (
        code1 int(10) primary key,
    LOC varchar(10) not null);

insert into loca values (1,     'AAA');
insert into loca values (2,     'BBB');
insert into loca values (3,     'CCC');
insert into loca values (4,     'DDD');

我得到的代码是

select CASE WHEN a.pn IS NULL THEN b.pn ELSE a.pn  END AS PN
, a.q
, b.c 
, a.LOC_ID
, b.LOC_ID

from
(select 
h.pn
, sum(qty) q
, h.LOC_ID
from 
history h

group by h.pn, h.LOC_ID) a
RIGHT JOIN 
(select 
r.pn
, count(sn) c
, r.LOC_ID
from 
rota r
group by r.pn, r.LOC_ID) b
on a.pn = b.pn WHERE a.LOC_ID = b.LOC_ID
order by a.pn;

上面的代码对于两个表中的所有PN都有效。问题是特定于表之一的值。我可以从JOIN中删除 WHERE 子句,但这不是corect。问题是-如何仅从一张桌子中获取历史记录和轮播中的所有PN。我在RIGHT JOIN方面有些运气,但是没有涵盖其他表中的唯一值。有人遇到过解决方案吗?

结果应如下表所示

 PN      LOC_ID     Count   Qty
 T1        1           1    1
 A1        2           1    2
 J1        3           1    3
 A2        4           1    1
 J2        1           2    2
 A3        2           1    3
 J3        3           1    4
777        4           1    NULL
 A1        1           1    1
 J2        2           2    NULL
 J2        3           2    4
 A1        4           1    3
 J2        1           2    2
 J2        2           2    NULL
 J2        3           2    4
999        2           NULL 3

1 个答案:

答案 0 :(得分:1)

使用另一个联接left并将其设置为union

select t.PN,t.q,t.c,t.LOC_ID,t.LOC_ID_b from 
(
  select CASE WHEN a.pn IS NULL THEN b.pn ELSE a.pn  END AS PN
, a.q
, b.c 
, a.LOC_ID
, b.LOC_ID as LOC_ID_b

from
(select 
h.pn
, sum(qty) q
, h.LOC_ID
from 
history h

group by h.pn, h.LOC_ID) a
RIGHT JOIN 
(select 
r.pn
, count(sn) c
, r.LOC_ID
from 
rota r
group by r.pn, r.LOC_ID) b
on a.pn = b.pn and a.LOC_ID = b.LOC_ID

) as t
union 
select t2.PN,t2.q,t2.c,t2.LOC_ID,t2.LOC_ID_b from
(
  select CASE WHEN a.pn IS NULL THEN b.pn ELSE a.pn  END AS PN
, a.q
, b.c 
, a.LOC_ID
, b.LOC_ID as LOC_ID_b

from
(select 
h.pn
, sum(qty) q
, h.LOC_ID
from 
history h

group by h.pn, h.LOC_ID) a
left JOIN 
(select 
r.pn
, count(sn) c
, r.LOC_ID
from 
rota r
group by r.pn, r.LOC_ID
) b
on a.pn = b.pn and a.LOC_ID = b.LOC_ID
) t2

http://sqlfiddle.com/#!9/c20c81/20