我无法连接多个表。我有一个查询仅在所有表中都包含数据时才有效。但是,相反,我需要执行left join
来显示空行。我确实有另一个使用left join
的查询,但无法正常工作。
最后,这也是我们数据库设计方式的问题,我将在下面解释。
首先,我有下表:
我的查询仅在所有表中都有数据时才起作用,这是由于使用了where
子句:
select c.company,
p.SKU,
p.carton_id,
mi.master_id,
m.length,
m.width,
m.height,
m.weight,
m.trackingid
from customer c,
products p,
master_ids mi,
manifest m
where c.ordernumber = p.ordernumber
and c.ordernumber = m.ordernumber
and (p.carton_id = mi.child_id or p.pick_id = mi.child_id)
and mi.master_id = m.master_id
and c.ordernumber = 4558689658;
我使用left join
重写了此查询。这是我到目前为止所拥有的,但是并不能按照我想要的方式工作:
select c.company,
p.SKU,
p.carton_id,
mi.master_id,
m.length,
m.width,
m.height,
m.weight,
m.trackingid
from customer c
left join products p
on c.ordernumber = p.ordernumber
left join master_ids mi
on p.carton_id = mi.child_id or p.pick_id = mi.child_id
left join manifest m
on mi.master_id = m.master_id
left join manifest m
on c.ordernumber = m.ordernumber
where c.ordernumber = 4558689658;
无论订单状态如何,left join
查询都需要向我显示数据。每个过程(拣配,包装,运输等)都会向上面的表中添加新数据,无论订单状态如何,我都希望看到它。
那么,如何重写第二个查询?我更喜欢先尝试一下。因此,请不要告诉我正确的查询,请告诉我我做错了什么以及纠正它需要做些什么。
答案 0 :(得分:0)
我不完全理解您的问题(不好),但我想向您提供建议。随意忽略它。我看到两件事:
<div class="col-sm-6">
<div class="well well-sm" style="background-color:lightcyan">
<h4>AVAS for Opperations</h4>
<div class="list-group">
<a href="https://go.microsoft.com/fwlink/?LinkID=398939" class="list-group-item" data-toggle="tooltip" title="(Make charts of buses passing timepoints!)">AVAS Time-Space Chart <img src="https://cdn1.iconfinder.com/data/icons/education-set-4/512/information-512.png" class="w3-round" alt="Denmark" style="width:3%"></a>
<a href="https://go.microsoft.com/fwlink/?LinkID=398939" class="list-group-item" data-toggle="tooltip" title="(Make a list of Route and Logon events from a bus, run or O/B)">History by Bus/Run/Operator <img src="https://cdn1.iconfinder.com/data/icons/education-set-4/512/information-512.png" class="w3-round" alt="Denmark" style="width:3%"></a>
<a asp-area="" asp-controller="Home" asp-action="Legal" class="list-group-item" data-toggle="tooltip" title="(Make a list of stops and timepoints for a bus for an hour)">Bus-Hour History <img src="https://cdn1.iconfinder.com/data/icons/education-set-4/512/information-512.png" class="w3-round" alt="Denmark" style="width:3%"></a>
<a asp-area="" asp-controller="Home" asp-action="Legal" class="list-group-item" data-toggle="tooltip" title="(Make a list of buses passing a selected stop)">Bus-Stop History <img src="https://cdn1.iconfinder.com/data/icons/education-set-4/512/information-512.png" class="w3-round" alt="Denmark" style="width:3%"></a>
</div>
</div>
</div>
表开始。然后,您使用products
联接其他表。LEFT JOIN
的{{1}}子句中,而不是ON
子句中。后者只能用于过滤主表上的条件。select c.company, p.SKU, p.carton_id, mi.master_id, m.length, m.width, m.height, m.weight, m.trackingid from products p -- Start with the table you want to always include left join customer c on c.ordernumber = p.ordernumber and c.ordernumber = 4558689658 -- filter a related table here left join master_ids mi on p.carton_id = mi.child_id or p.pick_id = mi.child_id left join manifest m on mi.master_id = m.master_id left join manifest m on c.ordernumber = m.ordernumber;