MySQL - 带子查询的select查询给出错误

时间:2012-10-22 07:15:35

标签: mysql

当我尝试使用子查询创建视图时,我遇到了mysql的问题

这是我想要制作视图的SQL脚本。

select 
          LOG_ID
          ,REF_ID
          ,TRX_ID
          ,TIME_EXEC
          ,STATUS
          ,RESULT_CODE
          ,ACCOUNT_NO
          ,AMOUNT
          ,REF_NO
          ,TASK_TYPE
          ,NAME_E
          ,NAME_I         
        from (
        select
          LT.LOG_ID
          ,LT.REF_ID
          ,LT.TRX_ID
          ,LT.TIME_EXEC
          ,LT.STATUS
          ,LT.RESULT_CODE
          ,TK.ACCOUNT_NO
          ,TK.AMOUNT
          ,TK.REF_NO
          ,TK.TASK_TYPE
          ,TT.NAME_E
          ,TT.NAME_I
        from
          IB_LOG_TRANSACTIONS LT, IB_TASKS TK, IB_TASK_TYPES TT
        where
          LT.TRX_ID is null
          and LT.REF_ID = TK.REF_ID
          and TK.TASK_TYPE = TT.TASK_TYPE
        union (
        select
          LT.LOG_ID
          ,LT.REF_ID
          ,LT.TRX_ID
          ,LT.TIME_EXEC
          ,LT.STATUS
          ,LT.RESULT_CODE
          ,T.ACCOUNT_NO
          ,T.AMOUNT
          ,TK.REF_NO
          ,TK.TASK_TYPE
          ,TT.NAME_E
          ,TT.NAME_I
        from
          IB_LOG_TRANSACTIONS LT, IB_TRANSFERS T, IB_TASKS TK, IB_TASK_TYPES TT
        where
          LT.TRX_ID = T.TRX_ID
          and LT.REF_ID = T.REF_ID
          and LT.REF_ID = TK.REF_ID
          and TK.TASK_TYPE = TT.TASK_TYPE
        ) union (
        select
          LT.LOG_ID
          ,LT.REF_ID
          ,LT.TRX_ID
          ,LT.TIME_EXEC
          ,LT.STATUS
          ,LT.RESULT_CODE
          ,TD.ACCOUNT_NO
          ,TD.AMOUNT
          ,TK.REF_NO
          ,TK.TASK_TYPE
          ,TT.NAME_E
          ,TT.NAME_I
        from
          IB_LOG_TRANSACTIONS LT, IB_TRANSFERS_DOM TD, IB_TASKS TK, IB_TASK_TYPES TT
        where
          LT.TRX_ID = TD.TRX_ID
          and LT.REF_ID = TD.REF_ID
          and LT.REF_ID = TK.REF_ID
          and TK.TASK_TYPE = TT.TASK_TYPE
        ) union (
        select
          LT.LOG_ID
          ,LT.REF_ID
          ,LT.TRX_ID
          ,LT.TIME_EXEC
          ,LT.STATUS
          ,LT.RESULT_CODE
          ,TF.ACCOUNT_NO
          ,TF.AMOUNT
          ,TK.REF_NO
          ,TK.TASK_TYPE
          ,TT.NAME_E
          ,TT.NAME_I
        from
          IB_LOG_TRANSACTIONS LT, IB_TRANSFERS_FGN TF, IB_TASKS TK, IB_TASK_TYPES TT
        where
          LT.TRX_ID = TF.TRX_ID
          and LT.REF_ID = TF.REF_ID
          and LT.REF_ID = TK.REF_ID
          and TK.TASK_TYPE = TT.TASK_TYPE
        ) union (
        select
          LT.LOG_ID
          ,LT.REF_ID
          ,LT.TRX_ID
          ,LT.TIME_EXEC
          ,LT.STATUS
          ,LT.RESULT_CODE
          ,P.ACCOUNT_NO
          ,P.AMOUNT
          ,TK.REF_NO
          ,TK.TASK_TYPE
          ,TT.NAME_E
          ,TT.NAME_I
        from
          IB_LOG_TRANSACTIONS LT, IB_PAYMENTS P, IB_TASKS TK, IB_TASK_TYPES TT
        where
          LT.TRX_ID = P.TRX_ID
          and LT.REF_ID = P.REF_ID
          and LT.REF_ID = TK.REF_ID
          and TK.TASK_TYPE = TT.TASK_TYPE
        ))

我需要一个很大的帮助,这是我的MYSQL脚本,我对MySQL并不熟悉。 谢谢你帮助我:)。

1 个答案:

答案 0 :(得分:3)

Restrictions on Views

您不能在视图的SELECT查询中使用子查询,这是MySQL中的限制。请尝试此查询 -

SELECT LT.LOG_ID
     , LT.REF_ID
     , LT.TRX_ID
     , LT.TIME_EXEC
     , LT.STATUS
     , LT.RESULT_CODE
     , TK.ACCOUNT_NO
     , TK.AMOUNT
     , TK.REF_NO
     , TK.TASK_TYPE
     , TT.NAME_E
     , TT.NAME_I
FROM
  IB_LOG_TRANSACTIONS LT, IB_TASKS TK, IB_TASK_TYPES TT
WHERE
  LT.TRX_ID IS NULL
  AND LT.REF_ID = TK.REF_ID
  AND TK.TASK_TYPE = TT.TASK_TYPE
UNION
SELECT LT.LOG_ID
     , LT.REF_ID
     , LT.TRX_ID
     , LT.TIME_EXEC
     , LT.STATUS
     , LT.RESULT_CODE
     , T.ACCOUNT_NO
     , T.AMOUNT
     , TK.REF_NO
     , TK.TASK_TYPE
     , TT.NAME_E
     , TT.NAME_I
FROM
  IB_LOG_TRANSACTIONS LT, IB_TRANSFERS T, IB_TASKS TK, IB_TASK_TYPES TT
WHERE
  LT.TRX_ID = T.TRX_ID
  AND LT.REF_ID = T.REF_ID
  AND LT.REF_ID = TK.REF_ID
  AND TK.TASK_TYPE = TT.TASK_TYPE
UNION
SELECT LT.LOG_ID
     , LT.REF_ID
     , LT.TRX_ID
     , LT.TIME_EXEC
     , LT.STATUS
     , LT.RESULT_CODE
     , TD.ACCOUNT_NO
     , TD.AMOUNT
     , TK.REF_NO
     , TK.TASK_TYPE
     , TT.NAME_E
     , TT.NAME_I
FROM
  IB_LOG_TRANSACTIONS LT, IB_TRANSFERS_DOM TD, IB_TASKS TK, IB_TASK_TYPES TT
WHERE
  LT.TRX_ID = TD.TRX_ID
  AND LT.REF_ID = TD.REF_ID
  AND LT.REF_ID = TK.REF_ID
  AND TK.TASK_TYPE = TT.TASK_TYPE
UNION
SELECT LT.LOG_ID
     , LT.REF_ID
     , LT.TRX_ID
     , LT.TIME_EXEC
     , LT.STATUS
     , LT.RESULT_CODE
     , TF.ACCOUNT_NO
     , TF.AMOUNT
     , TK.REF_NO
     , TK.TASK_TYPE
     , TT.NAME_E
     , TT.NAME_I
FROM
  IB_LOG_TRANSACTIONS LT, IB_TRANSFERS_FGN TF, IB_TASKS TK, IB_TASK_TYPES TT
WHERE
  LT.TRX_ID = TF.TRX_ID
  AND LT.REF_ID = TF.REF_ID
  AND LT.REF_ID = TK.REF_ID
  AND TK.TASK_TYPE = TT.TASK_TYPE
UNION
SELECT LT.LOG_ID
     , LT.REF_ID
     , LT.TRX_ID
     , LT.TIME_EXEC
     , LT.STATUS
     , LT.RESULT_CODE
     , P.ACCOUNT_NO
     , P.AMOUNT
     , TK.REF_NO
     , TK.TASK_TYPE
     , TT.NAME_E
     , TT.NAME_I
FROM
  IB_LOG_TRANSACTIONS LT, IB_PAYMENTS P, IB_TASKS TK, IB_TASK_TYPES TT
WHERE
  LT.TRX_ID = P.TRX_ID
  AND LT.REF_ID = P.REF_ID
  AND LT.REF_ID = TK.REF_ID
  AND TK.TASK_TYPE = TT.TASK_TYPE