我在这里遇到问题,当我尝试使用下面的查询进行选择时,我收到错误消息
错误代码:1248 每个派生表都必须有自己的别名
SELECT B.BRANCH_CODE, B.BRANCH_NAME, C.COMPANY_CODE, C.NAME, TSK.DATE_CREATE,
CASE TB.BULK_TYPE
WHEN 1 THEN 'Bulk Transfer'
WHEN 2 THEN 'Bulk Payment'
ELSE 'Payroll'
END AS TRX_METHOD_E,
CASE TB.BULK_TYPE
WHEN 1 THEN 'Bulk Transfer'
WHEN 2 THEN 'Bulk Pembayaran Tagihan'
ELSE 'Pembayaran Gaji'
END AS TRX_METHOD_I,
TB.TOTAL_RECORD,
TB.ACCOUNT_NO,
TSK.TRX_TOTAL,
TC.TOTAL_CHARGE,
DATE(TSK.DATE_TRX) AS DATE_TRX,
TB.REF_ID,
CASE
WHEN TSK.TRX_COUNT_SUCCESS = TSK.TRX_COUNT_ALL THEN 'All Success'
WHEN TSK.TRX_COUNT_FAIL = TSK.TRX_COUNT_ALL THEN 'All Failed'
WHEN TSK.TRX_COUNT_SUCCESS > 0 AND TSK.TRX_COUNT_FAIL > 0 THEN 'Partial Success (' || TSK.TRX_COUNT_SUCCESS || '/' || TSK.TRX_COUNT_ALL || ')'
ELSE 'Pending Execution'
END AS TRX_STATUS_E,
CASE
WHEN TSK.TRX_COUNT_SUCCESS = TSK.TRX_COUNT_ALL THEN 'Berhasil Semua'
WHEN TSK.TRX_COUNT_FAIL = TSK.TRX_COUNT_ALL THEN 'Gagal Semua'
WHEN TSK.TRX_COUNT_SUCCESS > 0 AND TSK.TRX_COUNT_FAIL > 0 THEN 'Berhasil Sebagian (' || TSK.TRX_COUNT_SUCCESS || '/' || TSK.TRX_COUNT_ALL || ')'
ELSE 'Tunggu Eksekusi'
END AS TRX_STATUS_I
FROM CB_TASK_BULKS TB
JOIN CB_TASKS TSK ON TSK.REF_ID = TB.REF_ID
JOIN CB_COMPANIES C ON C.COMPANY_ID = TSK.COMPANY_ID
JOIN CB_BRANCHES B ON B.BRANCH_CODE = C.BRANCH_CODE,
(
SELECT REF_ID, SUM(CHARGE) AS TOTAL_CHARGE
FROM
(
SELECT XTB.REF_ID, SUM(CHARGE) AS CHARGE
FROM CB_TRANSFERS XT
JOIN CB_TASK_BULK_DETAILS XTBD ON XTBD.BULK_DETAIL_ID = XT.BULK_DETAIL_ID
JOIN CB_TASK_BULKS XTB ON XTB.REF_ID = XTBD.REF_ID
GROUP BY XTB.REF_ID
UNION
SELECT XTB2.REF_ID, SUM(CHARGE) AS CHARGE
FROM CB_TRANSFERS_DOM XTD
JOIN CB_TASK_BULK_DETAILS XTBD2 ON XTBD2.BULK_DETAIL_ID = XTD.BULK_DETAIL_ID
JOIN CB_TASK_BULKS XTB2 ON XTB2.REF_ID = XTBD.REF_ID
GROUP BY XTB.REF_ID
UNION
SELECT XTB3.REF_ID, SUM(CHARGE) AS CHARGE
FROM CB_PAYMENTS XP
JOIN CB_TASK_BULK_DETAILS XTBD3 ON XTBD3.BULK_DETAIL_ID = XP.BULK_DETAIL_ID
JOIN CB_TASK_BULKS XTB3 ON XTB3.REF_ID = XTBD.REF_ID
GROUP BY XTB.REF_ID
)
GROUP BY REF_ID
) TC
WHERE TC.REF_ID = TSK.REF_ID
AND (TSK.TRX_COUNT_SUCCESS > 0 OR TSK.TRX_COUNT_FAIL > 0);
有人可以帮我在这里找到我的问题吗,我想我已经为所有表提供了他自己的“名称”,例如 TB for CB_BULKS_DATA(示例)
您需要为 MySQL 中的所有内联视图/派生表指定一个别名,即使它们是嵌套的。在您的代码中,您在另一个派生表中使用派生表,但您似乎忘记提及内部派生表的别名。在底部的第 4 行之前放置一个别名,即
GROUP BY REF_ID
. 在这个 group by 子句之前放一个别名,你会没事的。我已经解决了我的问题,
忘记联合表上的别名只是我的错
见别名