SQL LIKE 别名
这可能吗?
我正在尝试对别名执行 SQL 语句,但出现错误...
SELECT
CASE PERSON_TYPE WHEN 'PER' THEN LAST_NAME
ELSE COMPANY_NAME
END AS FULLNAME, *
FROM PERSON
[WHERE FULLNAME LIKE '%AN%'] // Error FULLNAME - INvalid COlumn Name
否则,这是我正在使用的替代方案...
WHERE (LAST_NAME LIKE '%AN%') OR (COMPANY_NAME LIKE '%AN%')
谢谢
Is this possible?
I'm trying to do a SQL statement on an alias but getting an error...
SELECT
CASE PERSON_TYPE WHEN 'PER' THEN LAST_NAME
ELSE COMPANY_NAME
END AS FULLNAME, *
FROM PERSON
[WHERE FULLNAME LIKE '%AN%'] // Error FULLNAME - INvalid COlumn Name
otherwise, this is the alternative I'm using....
WHERE (LAST_NAME LIKE '%AN%') OR (COMPANY_NAME LIKE '%AN%')
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
你有两个解决方案。
一种是将 FULLNAME 传递给 WHERE 子句,如下所示:
另一种是进行内部查询,如下所示:
You have two solutions.
One is to pass the FULLNAME to the WHERE CLAUSE like this:
THe other is to make an INNER QUERY like this:
你可以这样做的一种方法是
One way you could do it is
您无法访问
WHERE
中的字段别名。使用派生表/CTE 作为替代方案,或在WHERE
中重复整个表达式。mysql 的技巧:你可以将它放在
HAVING
上,而不使用GROUP BY
You cannot access field aliases in
WHERE
. Use derived tables/CTE as an alternative, or repeat the whole expression inWHERE
.Trick for mysql : you can put it on
HAVING
withoutGROUP BY
不可以,您不能在 WHERE 条件中使用别名。顺便说一句,您可能还想从 select 语句中删除“, *”。这也会导致错误。
No, you cannot use alias in WHERE condition. BTW, you might also want to remove ", *" from the select statement. That will also cause an error.