您有一个非常奇怪的查询形式。首先,您应该使用join
语法。其次,您似乎想要两个库存表之间的联合:
select d.msisdn, b.bdo_id
from (select i.*
from (select i.* from inventory i union all
select i2.* from inventory i2
) i
) i join
details d
on d.allocationid = i.allocationid join
bdo b
on i.bdo_id=b.bdo_id
where d.msisdn = 3454159650;
将查询结构化为显式连接应该使其更有效,并且应该更容易理解、正确和维护。
编辑:
您可能会丢失某些表中的某些记录。尝试将此版本与left outer join
:
select d.msisdn, b.bdo_id
from details d left outer join
(select i.*
from (select i.* from inventory i union all
select i2.* from inventory i2
) i
) i
details d
on d.allocationid = i.allocationid left outer join
bdo b
on i.bdo_id=b.bdo_id
where d.msisdn = 3454159650;