如何决定哪个可以作为第一张表,哪个可以作为第二张表?

huangapple go评论46阅读模式
英文:

how to decide which can be taken as first table and second table?

问题

以下是翻译好的部分:

原文:
To find the salesorders for all productsID along with productsID and name?

翻译:
要查找所有产品ID的销售订单,以及产品ID和名称?

原文:
I wrote this cause the sales table also has productsID column.

翻译:
我写这个是因为销售表也有产品ID列。

原文:
select A.salesorderID, A.productsID, B.Name
from Production.productsID AS A
LEFT JOIN sales.salesorderID AS B
ON A.productsID = B.proudctsID

翻译:
选择 A.salesorderID,A.productsID,B.Name
从 Production.productsID 作为 A
左连接 sales.salesorderID 作为 B
在 A.productsID = B.proudctsID 上

原文:
But the answer I saw is

翻译:
但我看到的答案是

原文:
select A.productsID, A.Name, B.salesorderID
from Production.productsID AS A
LEFT JOIN sales.salesorderID AS B
ON A.productsID = B.productsID

翻译:
选择 A.productsID,A.Name,B.salesorderID
从 Production.productsID 作为 A
左连接 sales.salesorderID 作为 B
在 A.productsID = B.productsID 上

英文:

To find the salesorders for all productsID along with productsID and name?

I wrote this cause the sales table also has productsID column.

select A.salesorderID, A.productsID, B.Name 
from Production.productsID AS A
LEFT JOIN sales.salesorderID AS B
ON A.productsID = B.proudctsID

But the answer I saw is

select A.productsID, A.Name, B.salesorderID
from Production.productsID AS A
LEFT JOIN sales.salesorderID AS B 
ON A.productsID = B.productsID

答案1

得分: 1

根据你想要的输出方式来做决定。左连接的目的是将一个拥有你想要查看的数值的表与另一个可能拥有你想要查看数值的表连接起来。连接顺序是根据你不想在查询中留下的数值而确定的逻辑顺序。

比方说,我有一个包含与宠物表关联的人员表。
**人员表**

|ID|姓名|
|-|-|
|1|约翰|
|2|迈克|
|3||

**宠物表**

|ID|PersonID|姓名|
|-|-|-|
|1|1|富多|
|2|2|巴奇|

我们的组织要举办一场遛狗活动,我们想邀请所有的人员。简单,对吧?完成。

```SQL
    SELECT * FROM 人员表;
ID 姓名
1 约翰
2 迈克
3

等等,这是一个遛狗活动,所以我想在邀请中包括这些人的宠物名字。没问题。

SELECT 人员表.*, 宠物表.姓名 AS 宠物姓名 FROM 人员表 INNER JOIN 宠物表 ON 宠物表.PersonID = 人员表.ID;

结果:

ID 姓名 宠物姓名
1 约翰 富多
2 迈克 巴奇

现在,我已经为所有人和他们的宠物准备好了邀请。

但是等等!主管希望不仅限于只有宠物的人。任何人都可以参加!这是一场步行马拉松,毕竟,并不只有有宠物的人才能步行和捐款支持我的事业。所以,现在我想获取所有的人员及其宠物的名字(如果有的话)。这就是你决定左连接顺序的方式。

    SELECT 人员表.*, 宠物.姓名 
    FROM 人员表 LEFT OUTER JOIN
        宠物 ON 宠物.PersonID = 人员表.ID;

现在我的结果如下。

ID 姓名 宠物姓名
1 约翰 富多
2 迈克 巴奇
3 NULL

<details>
<summary>英文:</summary>

The decision is made based on how you want your output. The purpose of a left join is to join a table that has values you want to see to another table that MIGHT have values you want to see. The order is logical depending on which values you DON&#39;T want to be left out of the query.

So, lets say I have a table of People with a relationship to a table of Pets.
**People**

|ID|Name|
|-|-|
|1|John|
|2|Mike|
|3|Sue|

**Pets**

|ID|PersonID|Name|
|-|-|-|
|1|1|Foodo|
|2|2|Barrky|

My organization is having a dog walking event and we want to invite all of our people. Simple, right? Done.

```Sql
    SELECT * FROM People;
ID Name
1 John
2 Mike
3 Sue

But wait, its a dog walking event, so I want to include the name of these peoples Pets on the invite. No big deal.

SELECT People.*, Pets.Name AS PetName FROM People INNER JOIN Pets on Pets.PersonID = People.ID;

Result:

ID Name PetName
1 John Foodo
2 Mike Barrky

Bam, now I have invites for all of my people and their pets.

But wait! The director doesn't want to limit this to ONLY people with pets. Anyone is invited! Its a walk-a-thon and after all, not ONLY people with pets can walk and donate money to my cause. So now, I want to get ALL of my people along with their Pet's name if they have one. THIS is how you decide which order the left join should be.

    SELECT People.*, Pet.Name 
    FROM People LEFT OUTER JOIN
        Pets ON Pet.PersonID = People.ID;

Now my results are the below.

ID Name PetName
1 John Foodo
2 Mike Barrky
3 Sue NULL

答案2

得分: 0

我猜测/假设 B.Name 不同于 A.Name。A.Name 可能是产品的名称(因为它来自产品表),而 B.Name 是销售订单中某物的名称。

英文:

I'm guessing/assuming that B.Name is not the same as A.Name. A.Name is presumably the name of the Product (as it is coming from the Product table) whereas B.Name is the name of something in the sales order

huangapple
  • 本文由 发表于 2023年2月14日 21:27:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/75448515.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定