比方说我下面有这个查询。
SELECT UserSub.Lastname
FROM (
SELECT UserId, Email, Firstname, Lastname as COL_1234
FROM Users
)
as UsersSub;
这将导致错误
Error Code: 1054. Unknown column 'UsersSub.Lastname' in 'field list'
我知道它不起作用,因为子查询未返回列Lastname
。当我将UsersSub.Lastname
更改为UsersSub.COL_1234
时,它可以正常工作。但是我需要一种基于别名检索原始列名的方法。子查询是由我无法更改的另一部分代码生成的,因此这对我没有影响。因为稍后需要在查询中添加where子句,并且需要在别名字段上进行搜索。因此,在即将发布的功能中,查询将如下所示。
SELECT UserSub.Lastname
FROM (
SELECT UserId, Email, Firstname, Lastname as COL_1234
FROM Users
)
as UsersSub
WHERE UserSub.Lastname LIKE '%son';
答案 0 :(得分:1)
我认为这会起作用:
SELECT u.Lastname
FROM (
SELECT UserId, Email, Firstname, Lastname as COL_1234
FROM Users
) AS UsersSub
LEFT JOIN ON Users AS u WHERE UsersSub.UserId = u.UserId
WHERE UserSub.Lastname LIKE '%son';
答案 1 :(得分:0)
尝试一下,对我来说很好
SELECT UserSub.Lastname
FROM (
SELECT UserId, Email, Firstname, Lastname, Lastname as COL_1234
FROM Users
)
as UsersSub
WHERE UserSub.Lastname LIKE '%son';