-1

当我尝试使用子查询创建视图时,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 不是很熟悉。谢谢你帮助我:)

4

1 回答 1

3

视图限制

您不能在视图的 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
于 2012-10-22T07:23:40.510 回答