英文:
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'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'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 || ' (' || 'age:' || person.age || ')' 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>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论