使用重复键将合并语句转换为 MYSQL
Conversion of merge statement to MYSQL using on duplicate key
请建议如何在 MYSQL 中转换此 teradata 语句。我们知道 mysql 不支持合并语句。在 select 查询中也使用了以下 2 table,并且我们在每个 table.
中有多个主键
MERGE INTO XYZ USING (
SELECT
ITRR.WORKFLOW_NAME WORKFLOW_NAME
, ITRR.INSTANCE_NAME INSTANCE_NAME
, MIN(ITRR.START_TIME) EARLIEST_START_TIME
, ITRR.SUBJECT_AREA SUBJECT_AREA
, 'INFORMATICA' PLATFORM_NAME
FROM
ABC IWRR
, DEF ITRR
WHERE
IWRR.WORKFLOW_RUN_ID = ITRR.WORKFLOW_RUN_ID
AND IWRR.USER_NAME IN ('xyz')
AND ITRR.RUN_STATUS_CODE <> 2
GROUP BY
ITRR.WORKFLOW_NAME
, ITRR.INSTANCE_NAME
, ITRR.SUBJECT_AREA
) SRC
ON
XYZ.PARENT_JOB_NAME = SRC.WORKFLOW_NAME
AND XYZ.CHILD_JOB_NAME = SRC.INSTANCE_NAME
AND XYZ.SANDBOX = SRC.SUBJECT_AREA
WHEN MATCHED THEN UPDATE SET FIRST_EXECUTION = SRC.EARLIEST_START_TIME
WHEN NOT MATCHED THEN INSERT
(
PARENT_JOB_NAME
, CHILD_JOB_NAME
, FIRST_EXECUTION
, SANDBOX
, PLATFORM_NAME
)VALUES
(
SRC.WORKFLOW_NAME
, SRC.INSTANCE_NAME
, SRC.EARLIEST_START_TIME
, SRC.SUBJECT_AREA
, SRC.PLATFORM_NAME
);
我正在尝试以下查询,但它不起作用。
INSERT INTO XYZ (
PARENT_JOB_NAME
, CHILD_JOB_NAME
, FIRST_EXECUTION
, SANDBOX
, PLATFORM_NAME
)
(SELECT
ITRR.WORKFLOW_NAME WORKFLOW_NAME
, ITRR.INSTANCE_NAME INSTANCE_NAME
, MIN(ITRR.START_TIME) EARLIEST_START_TIME
, ITRR.SUBJECT_AREA SUBJECT_AREA
, 'INFORMATICA' PLATFORM_NAME
FROM
ABC IWRR
, DEF ITRR
WHERE
IWRR.WORKFLOW_RUN_ID = ITRR.WORKFLOW_RUN_ID
AND IWRR.USER_NAME IN ('XYZ')
AND ITRR.RUN_STATUS_CODE <> 2
GROUP BY
ITRR.WORKFLOW_NAME
, ITRR.INSTANCE_NAME
, ITRR.SUBJECT_AREA
) SRC
ON DUPLICATE KEY UPDATE
FIRST_EXECUTION = SRC.EARLIEST_START_TIME
XYZ的主键=PARENT_JOB_NAME
ABC的主键=SUBJECT_ID
DEF的主键= SUBJECT_ID
,WORKFLOW_ID
,WORKFLOW_RUN_ID
,WORKLET_RUN_ID
,INSTANCE_ID
,TASK_ID
,START_TIME
MySQL 中的正确语法是:
INSERT INTO XYZ (PARENT_JOB_NAME, CHILD_JOB_NAME, FIRST_EXECUTION, SANDBOX, PLATFORM_NAME)
SELECT ITRR.WORKFLOW_NAME, ITRR.INSTANCE_NAME,
MIN(ITRR.START_TIME), ITRR.SUBJECT_AREA, 'INFORMATICA'
FROM ABC IWRR JOIN
DEF ITRR
ON IWRR.WORKFLOW_RUN_ID = ITRR.WORKFLOW_RUN_ID
WHERE IWRR.USER_NAME IN ('XYZ') AND
ITRR.RUN_STATUS_CODE <> 2
GROUP BY ITRR.WORKFLOW_NAME, ITRR.INSTANCE_NAME, ITRR.SUBJECT_AREA
ON DUPLICATE KEY UPDATE FIRST_EXECUTION = VALUES(FIRST_EXECUTION);
注意使用正确、明确、标准、可读的JOIN
语法。使用它。
主要变化是
- 修复过时的语法。
insert . . . select
中的 select
不需要删除括号(尽管它们可能是允许的)。
- 删除 table 别名,这是绝对不允许的。
- 修复
on duplicate key
语句。
相信@Akina 的评论是正确的,table XYZ 上的主键不正确。
XYZ 上的主键 table 需要包含这些列 PARENT_JOB_NAME, CHILD_JOB_NAME and SANDBOX
才能使 mysql INSERT ... ON DUPLICATE KEY
语句正常工作。
请建议如何在 MYSQL 中转换此 teradata 语句。我们知道 mysql 不支持合并语句。在 select 查询中也使用了以下 2 table,并且我们在每个 table.
中有多个主键MERGE INTO XYZ USING (
SELECT
ITRR.WORKFLOW_NAME WORKFLOW_NAME
, ITRR.INSTANCE_NAME INSTANCE_NAME
, MIN(ITRR.START_TIME) EARLIEST_START_TIME
, ITRR.SUBJECT_AREA SUBJECT_AREA
, 'INFORMATICA' PLATFORM_NAME
FROM
ABC IWRR
, DEF ITRR
WHERE
IWRR.WORKFLOW_RUN_ID = ITRR.WORKFLOW_RUN_ID
AND IWRR.USER_NAME IN ('xyz')
AND ITRR.RUN_STATUS_CODE <> 2
GROUP BY
ITRR.WORKFLOW_NAME
, ITRR.INSTANCE_NAME
, ITRR.SUBJECT_AREA
) SRC
ON
XYZ.PARENT_JOB_NAME = SRC.WORKFLOW_NAME
AND XYZ.CHILD_JOB_NAME = SRC.INSTANCE_NAME
AND XYZ.SANDBOX = SRC.SUBJECT_AREA
WHEN MATCHED THEN UPDATE SET FIRST_EXECUTION = SRC.EARLIEST_START_TIME
WHEN NOT MATCHED THEN INSERT
(
PARENT_JOB_NAME
, CHILD_JOB_NAME
, FIRST_EXECUTION
, SANDBOX
, PLATFORM_NAME
)VALUES
(
SRC.WORKFLOW_NAME
, SRC.INSTANCE_NAME
, SRC.EARLIEST_START_TIME
, SRC.SUBJECT_AREA
, SRC.PLATFORM_NAME
);
我正在尝试以下查询,但它不起作用。
INSERT INTO XYZ (
PARENT_JOB_NAME
, CHILD_JOB_NAME
, FIRST_EXECUTION
, SANDBOX
, PLATFORM_NAME
)
(SELECT
ITRR.WORKFLOW_NAME WORKFLOW_NAME
, ITRR.INSTANCE_NAME INSTANCE_NAME
, MIN(ITRR.START_TIME) EARLIEST_START_TIME
, ITRR.SUBJECT_AREA SUBJECT_AREA
, 'INFORMATICA' PLATFORM_NAME
FROM
ABC IWRR
, DEF ITRR
WHERE
IWRR.WORKFLOW_RUN_ID = ITRR.WORKFLOW_RUN_ID
AND IWRR.USER_NAME IN ('XYZ')
AND ITRR.RUN_STATUS_CODE <> 2
GROUP BY
ITRR.WORKFLOW_NAME
, ITRR.INSTANCE_NAME
, ITRR.SUBJECT_AREA
) SRC
ON DUPLICATE KEY UPDATE
FIRST_EXECUTION = SRC.EARLIEST_START_TIME
XYZ的主键=PARENT_JOB_NAME
ABC的主键=SUBJECT_ID
DEF的主键= SUBJECT_ID
,WORKFLOW_ID
,WORKFLOW_RUN_ID
,WORKLET_RUN_ID
,INSTANCE_ID
,TASK_ID
,START_TIME
MySQL 中的正确语法是:
INSERT INTO XYZ (PARENT_JOB_NAME, CHILD_JOB_NAME, FIRST_EXECUTION, SANDBOX, PLATFORM_NAME)
SELECT ITRR.WORKFLOW_NAME, ITRR.INSTANCE_NAME,
MIN(ITRR.START_TIME), ITRR.SUBJECT_AREA, 'INFORMATICA'
FROM ABC IWRR JOIN
DEF ITRR
ON IWRR.WORKFLOW_RUN_ID = ITRR.WORKFLOW_RUN_ID
WHERE IWRR.USER_NAME IN ('XYZ') AND
ITRR.RUN_STATUS_CODE <> 2
GROUP BY ITRR.WORKFLOW_NAME, ITRR.INSTANCE_NAME, ITRR.SUBJECT_AREA
ON DUPLICATE KEY UPDATE FIRST_EXECUTION = VALUES(FIRST_EXECUTION);
注意使用正确、明确、标准、可读的JOIN
语法。使用它。
主要变化是
- 修复过时的语法。
insert . . . select
中的select
不需要删除括号(尽管它们可能是允许的)。- 删除 table 别名,这是绝对不允许的。
- 修复
on duplicate key
语句。
相信@Akina 的评论是正确的,table XYZ 上的主键不正确。
XYZ 上的主键 table 需要包含这些列 PARENT_JOB_NAME, CHILD_JOB_NAME and SANDBOX
才能使 mysql INSERT ... ON DUPLICATE KEY
语句正常工作。