Mysql从UNION ALL查询结果集中查询对应列出现错误"Every derived table must have its own alias"

在使用UNION ALL联合表的时候出现了出现了Every derived table must have its own alias 错误

sql如下:

SELECT * FROM 
    (SELECT 1 CorpLevel,t.Operator_Id CorpId,t.Operator_Name CorpName,'1' CorpType FROM Fd_Operator t
         UNION ALL
     SELECT 2 CorpLevel,t.Con_Id CorpId,t.Con_Name CorpName,'2' CorpType FROM Fd_Consignor t
         UNION ALL
     SELECT 2 CorpLevel,t.Carrier_Id CorpId,t.Carrier_Name CorpName,'3' CorpType FROM Fd_Carrier t
         UNION ALL
     SELECT 3 CorpLevel,t.Ssa_Id CorpId,t.Ssa_Name CorpName,'4' CorpType FROM Fd_SupplySalesagency t) 
 WHERE CorpId='129'

出现如下错误:

1 queries executed, 0 success, 1 errors, 0 warnings

查询:select * from (select 1 CorpLevel,t.Operator_Id CorpId,t.Operator_Name CorpName,'1' CorpType from Fd_Operator t union all select...

错误代码: 1248
Every derived table must have its own alias

执行耗时   : 0 sec
传送时间   : 0 sec
总耗时      : 0.001 sec 
错误信息解析:

Every derived table must have its own alias
每个派生表必须有自己的别名
修改后的sql:

SELECT * FROM 
    (SELECT 1 CorpLevel,t.Operator_Id CorpId,t.Operator_Name CorpName,'1' CorpType FROM Fd_Operator t
         UNION ALL
     SELECT 2 CorpLevel,t.Con_Id CorpId,t.Con_Name CorpName,'2' CorpType FROM Fd_Consignor t
         UNION ALL
     SELECT 2 CorpLevel,t.Carrier_Id CorpId,t.Carrier_Name CorpName,'3' CorpType FROM Fd_Carrier t
         UNION ALL
     SELECT 3 CorpLevel,t.Ssa_Id CorpId,t.Ssa_Name CorpName,'4' CorpType FROM Fd_SupplySalesagency t) 
 AS a WHERE a.CorpId='129'


你可能感兴趣的:(mysql)