如何在左连接中使用别名?

时间:2017-03-17 03:06:52

标签: mysql left-join alias

我的MySQL命令有问题

select upper(file_type) as 'ItemType', ponum as 'Policy Number', office as 'Office', fullname as 'FullName', remarks as 'Remarks', concat(x.id, '-',x.file_type) as 'Identification'
from (select * from active_tb
union
select * from processed_tb) as x
left join filelocation as y on x.identification = y.f_id

我在尝试执行查询时得到unknown column x.Identification

首先,我连接两个不同的表,然后选择我需要的列,然后分配一个别名。我需要连接标识列。

但是我不能在左连接中使用别名。

2 个答案:

答案 0 :(得分:2)

首先在左连接子查询中构建您的标识列。

SELECT upper(file_type) AS 'ItemType',
       ponum            AS 'Policy Number',
       office           AS 'Office',
       fullname         AS 'FullName',
       remarks          AS 'Remarks',
       x.identification
FROM   (SELECT *,
               concat(x.id, '-', x.file_type) AS 'identification'
        FROM   active_tb
        UNION
        SELECT *,
               concat(x.id, '-', x.file_type) AS 'identification'
        FROM   processed_tb) AS x
       LEFT JOIN filelocation AS y
              ON x.identification = y.f_id 

答案 1 :(得分:0)

我现在解决了。 这是我的代码:

select ItemType, 'Policy Number', Office, fullname, identification, f_location
from (select upper(file_type) as 'ItemType', ponum as 'Policy Number', office as 'Office', fullname as 'FullName', remarks as 'Remarks', concat(id, '-',file_type) as 'Identification' from active_tb
union
select upper(file_type) as 'ItemType', ponum as 'Policy Number', office as 'Office', fullname as 'FullName', remarks as 'Remarks', concat(id, '-',file_type) as 'Identification' from processed_tb) as x
left join filelocation on filelocation.f_id = x.Identification

请评论其他建议。