SQL LIKE 别名

发布于 2025-01-05 22:36:12 字数 414 浏览 1 评论 0原文

这可能吗?

我正在尝试对别名执行 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

一曲琵琶半遮面シ 2025-01-12 22:36:12

你有两个解决方案。

一种是将 FULLNAME 传递给 WHERE 子句,如下所示:

SELECT 
      CASE PERSON_TYPE  WHEN 'PER' THEN LAST_NAME
                                   ELSE COMPANY_NAME
      END AS FULLNAME, PERSON.*
FROM PERSON
WHERE (PERSON_TYPE = 'PER' AND LAST_NAME LIKE '%AN%') OR (COMPANY_NAME LIKE '%AN%')

另一种是进行内部查询,如下所示:

SELECT t.*
FROM (SELECT *, CASE PERSON_TYPE  
                  WHEN 'PER' THEN LAST_NAME 
                  ELSE COMPANY_NAME END AS FULLNAME
FROM PERSON ) t
WHERE t.FULLNAME LIKE '%AN%'

You have two solutions.

One is to pass the FULLNAME to the WHERE CLAUSE like this:

SELECT 
      CASE PERSON_TYPE  WHEN 'PER' THEN LAST_NAME
                                   ELSE COMPANY_NAME
      END AS FULLNAME, PERSON.*
FROM PERSON
WHERE (PERSON_TYPE = 'PER' AND LAST_NAME LIKE '%AN%') OR (COMPANY_NAME LIKE '%AN%')

THe other is to make an INNER QUERY like this:

SELECT t.*
FROM (SELECT *, CASE PERSON_TYPE  
                  WHEN 'PER' THEN LAST_NAME 
                  ELSE COMPANY_NAME END AS FULLNAME
FROM PERSON ) t
WHERE t.FULLNAME LIKE '%AN%'
孤者何惧 2025-01-12 22:36:12

你可以这样做的一种方法是

SELECT *
FROM (SELECT *, CASE PERSON_TYPE  
                  WHEN 'PER' THEN LAST_NAME 
                  ELSE COMPANY_NAME END AS FULLNAME
FROM PERSON ) t
WHERE FULLNAME LIKE '%AN%'

One way you could do it is

SELECT *
FROM (SELECT *, CASE PERSON_TYPE  
                  WHEN 'PER' THEN LAST_NAME 
                  ELSE COMPANY_NAME END AS FULLNAME
FROM PERSON ) t
WHERE FULLNAME LIKE '%AN%'
翻身的咸鱼 2025-01-12 22:36:12

您无法访问 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 in WHERE.
Trick for mysql : you can put it on HAVING without GROUP BY

陌路黄昏 2025-01-12 22:36:12

不可以,您不能在 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文