3表之间的SQL查询关系
我有一个场景,其中有以下表:
库存
veliver_items
交付
我寻求一个查询,在其中拥有库存id
,我得到velivery_item
(fk_inventory), 然后,我从(fk_delivery)获得交货
。
手动,我转到delivery_items
表,然后搜索与我正在寻找的库存中匹配id
的fk_inventory
然后,我获得fk_delivery
,然后获取velrive
。 但是我需要对15k+项目进行报告。
如何写一个查询,从库存列表ids
我可以在上面提到的关系之后到达delovery
?
I have a scenario where I have the following tables:
Inventories
delivery_items
deliveries
I seek a query where, having the inventory id
, I get the delivery_item
(fk_inventory),
which then I get the delivery
from the (fk_delivery).
Manually, I go to the delivery_items
table, then I search for the fk_inventory
that matches the id
from the inventory that I'm looking for,
then I get the fk_delivery
, and get the delivery
.
But I need to run a report on 15k+ items.
How to write a query where from a list of inventory ids
I can get to the delivery
following the relationship that I mentioned above?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
编写SQL查询的网站很多,在正常(内部)与外部联接,左联接,右JOIN,子征服等之间区分了区分。简单的连接。
尝试这样思考,也许是这样做的。有一张纸,一个代表每个表格并在上面写下列。
现在,从视觉上查看可用的表,根据它们的相关方式将它们相互放置。请注意表A中的列,该列是下一个表的外键。再一次,从第二到第三。
完成此操作(甚至只是精神上),您就可以看到它们的关系。这是从条款中的基础
,因为您已经建立了所有关系,您始终可以从这些表中声明所需的各个列。像我在此处提供的别名易于使用,例如
yft
,yst
,ytt
表示第一,第二和第三个表。使用适合您表的别名,例如i =库存,di = delivery_items,d =送货。然后添加您想要的任何过滤条件。如果条件基于上面的第一张表,则将进入诸如
where 子句
yft.somecolumn ='blah'
如果过滤标准是您第二个或第三个表的特定于您的,只需将其添加到连接 /条件上,以便它与表保持在表格中,并且您知道它在上下文上与此相关联。当您进入左连接时,它使其更容易。
因此,现在,发动机可以将所有库存项目,相应的细节转到实际交付中,而无需每次进行单独的搜索,这会很痛苦地跟踪 /运行 /和性能。
There are many sites on writing SQL queries, differentiating between a normal (inner) join vs outer join, left join, right join, subqueries, etc. What you are looking to do is probably best (due to all inventory items in question) is simple joins.
Try to think of it this way, and maybe do it this way. Have a sheet of paper, one representing each table and write the columns on it.
Now, visually looking at the available tables, put them next to each other based on how they are related. Note the column in table A that is the foreign key to the next table. Then again, from the second to the third.
Once you have this done (or even if just mentally), you can SEE how they are related. This is the basis of the FROM clause
Now that you have all your relationships established, you can always declare the individual columns you want from those respective tables. Easier to use with the aliases such as I provided here via
yft
,yst
,ytt
representing the first, second and third tables. Use aliases appropriate to your tables such as i=inventories, di = delivery_items, d = deliveries.Then add whatever FILTERING conditions you want. If the condition is based on the FIRST Table such as yft above, that would go into the WHERE clause such as
where
yft.SomeColumn = 'blah'
If the filtering criteria is specific to your second or third table, just add that to the JOIN / ON condition so it stays with the table and you know contextually it is associated HERE. It makes it easier when you are getting into LEFT JOINs.
So now, the engine can go through all inventory items, to the corresponding details, to the actual deliveries without having to do individual searches each time which would be painful to trace / run / and performance.