自然连接的使用

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

Use of NATURAL JOIN

问题

根据我正在进行的任务,我需要将INNER JOIN更改为NATURAL JOIN,并获得与此[内连接的结果](https://i.stack.imgur.com/MXQvI.png)相同的结果。

我使用以下查询得到了那个结果:

SELECT person_order.order_date,
    person.name || ' (' || 'age:' || person.age || ')' AS person_information
FROM person_order
    INNER JOIN person ON person_order.person_id = person.id
ORDER BY 1,
    2;

这是我的表的样子我的表

我正在尝试在FROM语句中使用子查询,但现在结果与以前的查询不同不同的结果

SELECT pers_ord.order_date,
person.name || ' (' || 'age:' || person.age || ')' AS person_information
FROM (
SELECT order_date
FROM person_order
) AS pers_ord
NATURAL JOIN person
ORDER BY 1,
2;


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

According by the task I&#39;m doing I need to change INNER JOIN to NATURAL JOIN and get the same result as this [result of inner join](https://i.stack.imgur.com/MXQvI.png)

I got that result with this query:

SELECT person_order.order_date,
person.name || ' (' || 'age:' || person.age || ')' AS person_information
FROM person_order
INNER JOIN person ON person_order.person_id = person.id
ORDER BY 1,
2;


Here is what my tables look like [my tables](https://i.stack.imgur.com/H87ut.png)

I&#39;m trying to use subquery in a FROM statement but now result is differ with previous query [different rusult](https://i.stack.imgur.com/6puXC.png)

SELECT pers_ord.order_date,
person.name || ' (' || 'age:' || person.age || ')' AS person_information
FROM (
SELECT order_date
FROM person_order
) AS pers_ord
NATURAL JOIN person
ORDER BY 1,
2;


</details>


# 答案1
**得分**: 1

SELECT pers_ord.order_date,
    person.name || ' (' || '年龄:' || person.age || ')' AS 个人信息
FROM (
        SELECT order_date, person_id AS id
        FROM person_order
    ) AS pers_ord
    自然连接 person
ORDER BY 1,
    2;

在这里查看示例 [1],您可以看到执行连接操作的列需要相同。在您的 person_order 表中,person_id 列与 person 表中的 id 不匹配。


  [1]: https://www.postgresqltutorial.com/postgresql-tutorial/postgresql-natural-join/

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

Could you try this:

    SELECT pers_ord.order_date,
        person.name || &#39; (&#39; || &#39;age:&#39; || person.age || &#39;)&#39; AS person_information
    FROM (
            SELECT order_date, person_id AS id
            FROM person_order
        ) AS pers_ord
        NATURAL JOIN person
    ORDER BY 1,
        2;

Looking at examples [here][1] you can see the columns on which the join is performed need to be the same. And in your person_order table the person_id column is not matching the id in the person table.


  [1]: https://www.postgresqltutorial.com/postgresql-tutorial/postgresql-natural-join/

</details>



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

发表评论

匿名网友

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

确定