简体   繁体   English

3张表之间的SQL查询关系

[英]SQL Query relationship between 3 tables

I have a scenario where I have the following tables:我有一个场景,其中有以下表格:

Inventories delivery_items deliveries 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).我寻求一个查询,在哪里获得库存id ,我得到delivery_item (fk_inventory),然后我从 (fk_delivery) 获得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 .手动,我转到delivery_items表,然后从我正在寻找的库存中搜索与id匹配的fk_inventory ,然后我得到fk_delivery ,并得到delivery But I need to run a report on 15k+ items.但我需要对 15k+ 项进行报告。

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?根据我上面提到的关系,如何编写一个查询,我可以从库存ids列表中的哪个位置到达delivery

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.有很多关于编写 SQL 查询的网站,区分普通(内部)联接与外部联接、左联接、右联接、子查询等。您想要做的可能是最好的(由于所有库存项目有问题)是简单的连接。

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.注意表 A 中的列,它是下一个表的外键。 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这是 FROM 子句的基础

select *
   from
      YourFirstTable yft
         JOIN YourSecondTable yst
            on yft.WhateverKey = yst.MatchingKeyColumn
            JOIN YourThirdTable ytt
               on yst.KeyToThirdTable = ytt.KeyInThisTable

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.更容易使用别名,例如我在此处通过yftystytt的代表第一、第二和第三个表的别名。 Use aliases appropriate to your tables such as i=inventories, di = delivery_items, d = deliveries.使用适合您的表的别名,例如 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如果条件基于 FIRST 表,例如上面的 yft,则将进入 WHERE 子句,例如

where yft.SomeColumn = 'blah'其中 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.如果过滤条件特定于您的第二个或第三个表,只需将其添加到 JOIN / ON 条件中,以便它与表保持一致,并且您可以根据上下文知道它与 HERE 相关联。 It makes it easier when you are getting into LEFT JOINs.当您进入 LEFT JOIN 时,它会更容易。

   from
      YourFirstTable yft
         JOIN YourSecondTable yst
            on yft.WhateverKey = yst.MatchingKeyColumn
            AND yst.SecondTableColumn = 'someOtherValue'
            AND yst.SomeOtherColumn = 'somethingElse'

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.因此,现在,引擎可以遍历所有库存项目、相应的详细信息、实际交付,而不必每次都进行单独的搜索,这对于跟踪/运行/和性能来说是很痛苦的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM