2

我有一个 SQL 语句 - 对我来说无法解释 - 奇怪的行为。也许你会发现哪里出了问题:

当我使用语句

select count(*) from department

我得到了 2755 个结果

使用以下语句

select 
      building1.street, building1.streetno, building1.plz, building1.city, dept1.buildingid
  from 
      department dept1
    left join 
      supporter sup 
    on 
      dept.supporterid = sup.id
    left join 
      building building1
    on 
      sup.buildingid = building1.ibuildingid
  where 
      dept.usepostaladresssupporter = 1 
  union all
  select 
      building2.street, building2.streetno, building2.plz, building2.city, dept2.buildingid
  from 
      building building2
    right join 
      tueks_department dept2 
    on 
      dept2.buildingid = building2.ibuildingid
  where 
      dept2.usepostaladresssupporter = 0

我也得到了 2755 个结果。但是当我想将这两个语句与左连接结合起来时:

select count(*) from department
  left join
    (
      select 
          building1.street, building1.streetno, building1.plz, building1.city, dept1.buildingid
      from 
          department dept1
        left join 
          supporter sup 
        on 
          dept.supporterid = sup.id
        left join 
          building building1
        on 
          sup.buildingid = building1.ibuildingid
      where 
          dept.usepostaladresssupporter = 1 
      union all
      select 
          building2.street, building2.streetno, building2.plz, building2.city, dept2.buildingid
      from 
          building building2
        right join 
          tueks_department dept2 
        on 
          dept2.buildingid = building2.ibuildingid
      where 
          dept2.usepostaladresssupporter = 0
    ) postadress
  on
     department.buildingid = postadress.buildingid;

我得到了 3648513 个结果。

我的期望是,我只得到 2755 个结果。哪里错了?

感谢帮助!

4

2 回答 2

3

我认为这buildingid不是唯一的(根据我的推理,它不可能是唯一的)

想象一下简单的表格

表A

create TableA (name VARCHAR(32));
insert into TableA values ('Lieven');
insert into TableA values ('Lieven');      

表B

create TableB (name VARCHAR(32));
insert into TableB values ('Lieven');
insert into TableB values ('Lieven');
insert into TableB values ('AnyOtherValue');

选择语句

select * from TableA a left outer join TableB b on a.name = b.name

由于每条记录TableA都与每条记录匹配TableBname这将导致 4 条记录(AnyOtherValue 因不匹配而被忽略)

  • 的第一TableA条记录与“TableB”的三个记录中的两个一起返回
  • 的第二TableA条记录与“TableB”的三个记录中的两个一起返回
于 2013-01-24T14:06:09.970 回答
1

查询

select 
      building1.street, building1.streetno, building1.plz, building1.city, dept1.buildingid
  from 
      department dept1
    left join 
      supporter sup 
    on 
      dept.supporterid = sup.id
    left join 
      building building1
    on 
      sup.buildingid = building1.ibuildingid
  where 
      dept.usepostaladresssupporter = 1 
  union all
  select 
      building2.street, building2.streetno, building2.plz, building2.city, dept2.buildingid
  from 
      building building2
    right join 
      tueks_department dept2 
    on 
      dept2.buildingid = building2.ibuildingid
  where 
      dept2.usepostaladresssupporter = 0

每个部门将返回一行,其中 usepostaladresssupporter 为 0 或 1(请注意,不包括具有其他值的记录,这可能是也可能不是问题,具体取决于此列的约束)。

此查询结果的唯一键可能类似于 departmentid(您需要在选择条件中包含该列)。

所以正确的查询应该是这样的:

select * from department
  left join
    (
      select 
          building1.street, building1.streetno, building1.plz, building1.city, dept1.departmentid
      from 
          department dept1
        left join 
          supporter sup 
        on 
          dept.supporterid = sup.id
        left join 
          building building1
        on 
          sup.buildingid = building1.ibuildingid
      where 
          dept.usepostaladresssupporter = 1 
      union all
      select 
          building2.street, building2.streetno, building2.plz, building2.city, dept2.departmentid
      from 
          building building2
        right join 
          tueks_department dept2 
        on 
          dept2.buildingid = building2.ibuildingid
      where 
          dept2.usepostaladresssupporter = 0
    ) postadress
  on
     department.departmentid = postadress.departmentid;

您的查询将在这样的数据上出错:

Departmentid BuildingId 名称

1 1 部门 1

2 2 部门 2

3 2 部门3

乘法效果并不完全等于deptcount * deptcount,而是buildingcount * buildingcount + deptcount - buildingcount

于 2013-01-24T14:18:46.863 回答