gpt4 book ai didi

sql - 多表条件插入何时需要别名?

转载 作者:搜寻专家 更新时间:2023-10-30 19:49:27 25 4
gpt4 key购买 nike

我的 Oracle SQL 认证专家考试学习指南中的一个例子让我感到困惑。

SQL语句:

INSERT
WHEN (BOSS_SALARY-EMPLOYEE_SALARY < 10000) THEN
INTO SALARY_CHART (EMP_TITLE, SUPERIOR, EMP_INCOME, SUP_INCOME)
VALUES (EMPLOYEE, BOSS, EMPLOYEE_SALARY, BOSS_SALARY)
SELECT A.POSITION EMPLOYEE
, B.POSITION BOSS
, A.MAX_SALARY EMPLOYEE_SALARY
, B.MAX_SALARY BOSS_SALARY
FROM POSITIONS A
JOIN POSITIONS B ON A.REPORTS_TO_POSITION_ID = B.POSITION_ID

书中的解释:

Note that this version has done more than is required, and applies column aliases to each column in the subquery, then references those column aliases from the WHEN and VALUES clauses. We only needed column aliases on A.POSITION and B.POSITION in lines 5 and 6, so we can reference the column aliases in line 4.

我的困惑来源:

我们是否也需要在第 7 行和第 8 行中选择的两个 MAX_SALARY 列的别名,以便在第 4 行中区分它们?该解释似乎表明 EMPLOYEE_SALARY 和 BOSS_SALARY 别名是无关的...

有人可以澄清一下吗?

最佳答案

我猜他们的意思是他们只需要在两列(例如 A.POSITION 和 A.MAX_SALARY)上使用它来消除解析器的歧义。例如,这会起作用:

INSERT
WHEN (MAX_SALARY-EMPLOYEE_SALARY < 10000) THEN
INTO SALARY_CHART (EMP_TITLE, SUPERIOR, EMP_INCOME, SUP_INCOME)
VALUES (EMPLOYEE, POSITION, EMPLOYEE_SALARY, MAX_SALARY)
SELECT A.POSITION EMPLOYEE
, B.POSITION
, A.MAX_SALARY EMPLOYEE_SALARY
, B.MAX_SALARY
FROM POSITIONS A
JOIN POSITIONS B ON A.REPORTS_TO_POSITION_ID = B.POSITION_ID

关于sql - 多表条件插入何时需要别名?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6117863/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com