MySQL - create view using subquery in FROM clause

2019-02-23 16:59发布

I want to create view in MySQL using a subquery

CREATE OR REPLACE VIEW `V_TASK_TRANSFER` (`REF_ID`, `DATE_CREATE`, `DATE_TRX`, 
    `ACCOUNT_NO`, `TO_ACCOUNT_NO`, `TO_NAME`, `CURRENCY_CODE`, `AMOUNT`,
    `TASK_TYPE`, `NAME_E`, `NAME_I`, `REF_NO`, `EXECUTION_TYPE`,
    `REVIEW_COUNT`, `REVIEW_NEED`, `APPROVE_COUNT`, `APPROVE_NEED`, 
    `TRX_COUNT_SUCCESS`, `TRX_COUNT_FAIL`, `TRX_COUNT_ALL`,
    `STATUS_TF`, `USER_ID`
) AS 
  SELECT REF_ID,
    DATE_CREATE,
    DATE_TRX,
    ACCOUNT_NO,
    TO_ACCOUNT_NO,
    TO_NAME,
    CURRENCY_CODE,
    AMOUNT,
    TASK_TYPE,
    NAME_E,
    NAME_I,
    REF_NO,
    EXECUTION_TYPE,
    REVIEW_COUNT,
    REVIEW_NEED,
    APPROVE_COUNT,
    APPROVE_NEED,
    TRX_COUNT_SUCCESS,
    TRX_COUNT_FAIL,
    TRX_COUNT_ALL,
    STATUS_TF,
    USER_ID
  FROM
    (SELECT T.REF_ID,
      T.DATE_CREATE,
      T.DATE_TRX,
      TTF.ACCOUNT_NO,
      TTF.TO_ACCOUNT_NO,
      TTF.TO_NAME,
      TTF.CURRENCY_CODE,
      TTF.AMOUNT,
      T.TASK_TYPE,
      TT.NAME_E,
      TT.NAME_I,
      T.REF_NO,
      T.EXECUTION_TYPE,
      T.REVIEW_COUNT,
      T.REVIEW_NEED,
      T.APPROVE_COUNT,
      T.APPROVE_NEED,
      T.TRX_COUNT_SUCCESS,
      T.TRX_COUNT_FAIL,
      T.TRX_COUNT_ALL,
      T.STATUS_TF,
      T.USER_ID
    FROM IB_TASKS T
    JOIN IB_TASK_TRANSFERS TTF ON T.REF_ID = TTF.REF_ID
    JOIN IB_TASK_TYPES TT ON TT.TASK_TYPE = T.TASK_TYPE
    UNION
    SELECT T.REF_ID,
      T.DATE_CREATE,
      T.DATE_TRX,
      TTF.ACCOUNT_NO,
      TTF.TO_ACCOUNT_NO,
      TTF.TO_NAME,
      TTF.CURRENCY_CODE,
      TTF.AMOUNT,
      T.TASK_TYPE,
      TT.NAME_E,
      TT.NAME_I,
      T.REF_NO,
      T.EXECUTION_TYPE,
      T.REVIEW_COUNT,
      T.REVIEW_NEED,
      T.APPROVE_COUNT,
      T.APPROVE_NEED,
      T.TRX_COUNT_SUCCESS,
      T.TRX_COUNT_FAIL,
      T.TRX_COUNT_ALL,
      T.STATUS_TF,
      T.USER_ID
    FROM IB_TASKS T
    JOIN IB_TASK_TRANSFERS_DOM TTF ON T.REF_ID = TTF.REF_ID
    JOIN IB_TASK_TYPES TT ON TT.TASK_TYPE = T.TASK_TYPE
    UNION
    SELECT T.REF_ID,
      T.DATE_CREATE,
      T.DATE_TRX,
      TTF.ACCOUNT_NO,
      TTF.TO_ACCOUNT_NO,
      TTF.TO_NAME,
      TTF.CURRENCY_CODE,
      TTF.AMOUNT,
      T.TASK_TYPE,
      TT.NAME_E,
      TT.NAME_I,
      T.REF_NO,
      T.EXECUTION_TYPE,
      T.REVIEW_COUNT,
      T.REVIEW_NEED,
      T.APPROVE_COUNT,
      T.APPROVE_NEED,
      T.TRX_COUNT_SUCCESS,
      T.TRX_COUNT_FAIL,
      T.TRX_COUNT_ALL,
      T.STATUS_TF,
      T.USER_ID
    FROM IB_TASKS T
    JOIN IB_TASK_TRANSFERS_FGN TTF ON T.REF_ID = TTF.REF_ID
    JOIN IB_TASK_TYPES TT ON TT.TASK_TYPE = T.TASK_TYPE
    );

but it gave me

Error #1349 - View's SELECT contains a subquery in the FROM clause

How can I work around this limitation?

2条回答
Summer. ? 凉城
2楼-- · 2019-02-23 17:20

Try to do it without subquery -

CREATE OR REPLACE VIEW V_TASK_TRANSFER AS
    SELECT T.REF_ID,
      T.DATE_CREATE,
      T.DATE_TRX,
      TTF.ACCOUNT_NO,
      TTF.TO_ACCOUNT_NO,
      TTF.TO_NAME,
      TTF.CURRENCY_CODE,
      TTF.AMOUNT,
      T.TASK_TYPE,
      TT.NAME_E,
      TT.NAME_I,
      T.REF_NO,
      T.EXECUTION_TYPE,
      T.REVIEW_COUNT,
      T.REVIEW_NEED,
      T.APPROVE_COUNT,
      T.APPROVE_NEED,
      T.TRX_COUNT_SUCCESS,
      T.TRX_COUNT_FAIL,
      T.TRX_COUNT_ALL,
      T.STATUS_TF,
      T.USER_ID
    FROM IB_TASKS T
    JOIN IB_TASK_TRANSFERS TTF ON T.REF_ID = TTF.REF_ID
    JOIN IB_TASK_TYPES TT ON TT.TASK_TYPE = T.TASK_TYPE
    UNION
    SELECT T.REF_ID,
      T.DATE_CREATE,
      T.DATE_TRX,
      TTF.ACCOUNT_NO,
      TTF.TO_ACCOUNT_NO,
      TTF.TO_NAME,
      TTF.CURRENCY_CODE,
      TTF.AMOUNT,
      T.TASK_TYPE,
      TT.NAME_E,
      TT.NAME_I,
      T.REF_NO,
      T.EXECUTION_TYPE,
      T.REVIEW_COUNT,
      T.REVIEW_NEED,
      T.APPROVE_COUNT,
      T.APPROVE_NEED,
      T.TRX_COUNT_SUCCESS,
      T.TRX_COUNT_FAIL,
      T.TRX_COUNT_ALL,
      T.STATUS_TF,
      T.USER_ID
    FROM IB_TASKS T
    JOIN IB_TASK_TRANSFERS_DOM TTF ON T.REF_ID = TTF.REF_ID
    JOIN IB_TASK_TYPES TT ON TT.TASK_TYPE = T.TASK_TYPE
    UNION
    SELECT T.REF_ID,
      T.DATE_CREATE,
      T.DATE_TRX,
      TTF.ACCOUNT_NO,
      TTF.TO_ACCOUNT_NO,
      TTF.TO_NAME,
      TTF.CURRENCY_CODE,
      TTF.AMOUNT,
      T.TASK_TYPE,
      TT.NAME_E,
      TT.NAME_I,
      T.REF_NO,
      T.EXECUTION_TYPE,
      T.REVIEW_COUNT,
      T.REVIEW_NEED,
      T.APPROVE_COUNT,
      T.APPROVE_NEED,
      T.TRX_COUNT_SUCCESS,
      T.TRX_COUNT_FAIL,
      T.TRX_COUNT_ALL,
      T.STATUS_TF,
      T.USER_ID
    FROM IB_TASKS T
    JOIN IB_TASK_TRANSFERS_FGN TTF ON T.REF_ID = TTF.REF_ID
    JOIN IB_TASK_TYPES TT ON TT.TASK_TYPE = T.TASK_TYPE;
查看更多
霸刀☆藐视天下
3楼-- · 2019-02-23 17:28

As per documentation:

MySQL Docs

  • The SELECT statement cannot contain a subquery in the FROM clause.

Your workaround would be to create a view for each of your subqueries.

Then access those views of subqueries within your final view.

See solution to it

查看更多
登录 后发表回答