MySQL(+)的MySQL变体标志为外连接?

时间:2012-02-03 13:13:54

标签: mysql sql subquery outer-join database-migration

有人可以帮我重写这个SQL for MySQL数据库(有子查询和外连接)

**select REPORT_PACKAGE.SUBJECT_LINE, ACTIVATION_DT, PARAM_VALUE, REPORT_URL, hasCustomerDetails,
isAccountsRolledUp FROM REPORT_PACKAGE, REPORT_PARAMETER, REPORT_DEFINITION, REPORT_PACKAGE_CONTENT,
(select param_value as hasCustomerDetails, rpt_package_id from report_parameter
where param_name = 'hasCustomerDetails') a,
(select param_value isAccountsRolledUp, rpt_package_id rid from report_parameter
where param_name = 'isAccountsRolledUp') as b
WHERE REPORT_PACKAGE.RPT_PACKAGE_ID = REPORT_PARAMETER.RPT_PACKAGE_ID
and param_name = 'rulePlanId' and
REPORT_PACKAGE_CONTENT.RPT_PACKAGE_ID = REPORT_PACKAGE.RPT_PACKAGE_ID and
REPORT_PACKAGE_CONTENT.RPT_DEF_ID = REPORT_DEFINITION.RPT_DEF_ID AND
REPORT_URL like '%AcctNbr%' and REPORT_PACKAGE.RPT_PACKAGE_ID = a.RPT_PACKAGE_ID(+)
and REPORT_PACKAGE.RPT_PACKAGE_ID = b.RPT_PACKAGE_ID(+);**

我尝试了以下操作但它没有用:

**select repa.SUBJECT_LINE, ACTIVATION_DT, PARAM_VALUE, REPORT_URL, hasCustomerDetails,
isAccountsRolledUp FROM REPORT_PACKAGE repa
left join (select param_value as hasCustomerDetails, rpt_package_id from report_parameter
where param_name = 'hasCustomerDetails') as a
left join (select param_value as isAccountsRolledUp, rpt_package_id from report_parameter
where param_name = 'isAccountsRolledUp') as b
left join a on repa.RPT_PACKAGE_ID = a.RPT_PACKAGE_ID
left join b on repa.RPT_PACKAGE_ID = b.RPT_PACKAGE_ID
inner join REPORT_PACKAGE_CONTENT repc on repa.RPT_PACKAGE_ID = repc.RPT_PACKAGE_ID
inner join REPORT_DEFINITION rd on repc.RPT_DEF_ID = rd.RPT_DEF_ID
where REPORT_URL like '%AcctNbr%'**

1 个答案:

答案 0 :(得分:0)

我试图重写查询,这就是我所拥有的 -

SELECT
  REPORT_PACKAGE.SUBJECT_LINE,
  ACTIVATION_DT,
  PARAM_VALUE,
  REPORT_URL,
  hasCustomerDetails,
  isAccountsRolledUp
FROM REPORT_PACKAGE
  JOIN REPORT_PARAMETER
    ON REPORT_PARAMETER.RPT_PACKAGE_ID = REPORT_PACKAGE.RPT_PACKAGE_ID
  JOIN REPORT_PACKAGE_CONTENT
    ON REPORT_PACKAGE_CONTENT.RPT_PACKAGE_ID = REPORT_PACKAGE.RPT_PACKAGE_ID
  JOIN REPORT_DEFINITION
    ON REPORT_DEFINITION.RPT_DEF_ID = REPORT_PACKAGE_CONTENT.RPT_DEF_ID
  LEFT JOIN (SELECT param_value hasCustomerDetails, rpt_package_id FROM report_parameter WHERE param_name = 'hasCustomerDetails') a
    ON a.RPT_PACKAGE_ID = REPORT_PACKAGE.RPT_PACKAGE_ID
  LEFT JOIN (SELECT param_value isAccountsRolledUp, rpt_package_id rid FROM report_parameter WHERE param_name = 'isAccountsRolledUp') b
    ON b.RPT_PACKAGE_ID = REPORT_PACKAGE.RPT_PACKAGE_ID
WHERE
  param_name = 'rulePlanId'
  AND REPORT_URL like '%AcctNbr%'

添加表别名以使此查询更具可读性。