gpt4 book ai didi

mysql - Column Unkown With UPPER in HAVING clause for Column alias

转载 作者:行者123 更新时间:2023-11-30 23:20:55 24 4
gpt4 key购买 nike

我有一个 Web 应用程序,它使用 DataTables 进行服务器端处理。我有一些 SQL 查询可以任意重新组合列中的数据,所以我得到这样的查询:

SELECT case `column`
WHEN 'value1' then 'group1'
WHEN 'value2' then 'group1'
ELSE `column` END AS `column grouped`, SUM(`2010`)
GROUP BY `colonne grouped`

现在,我添加了一些基于用户输入的过滤,我这样做:

SELECT case `column`
WHEN 'value1' then 'group1'
WHEN 'value2' then 'group1'
ELSE `column` END AS `column grouped`, SUM(`2010`)
GROUP BY `column grouped`
HAVING `column grouped` LIKE '%test%'

它仍然工作得很好,但是当我试图让整个事情变得不敏感时,我的麻烦就来了,所以我这样做了:

SELECT case `column`
WHEN 'value1' then 'group1'
WHEN 'value2' then 'group1'
ELSE `column` END AS `column grouped`, SUM(`2010`)
GROUP BY `colonne grouped`
HAVING UPPER(`column grouped`) LIKE '%test%'

现在我得到“#1054 - Unknown column 'column grouped' in 'having clause'”

作为解决方法,我会这样做:

SELECT case `column`
WHEN 'value1' then 'group1'
WHEN 'value2' then 'group1'
ELSE `column` END AS `column grouped` , SUM(`2010`)
GROUP BY `colonne grouped`
HAVING UPPER(case `column`
WHEN 'value1' then 'group1'
WHEN 'value2' then 'group1'
ELSE `column` END) LIKE '%test%'

但这并不是很方便。任何人都知道为什么我不能将 UPPER 与自定义别名一起使用?

顺便说一下,我正在使用 MySQL 5.5。

最佳答案

根据 docs您不能在 HAVING 子句中使用函数:

The SQL standard requires that HAVING must reference only columns in the GROUP BY clause or columns used in aggregate functions. However, MySQL supports an extension to this behavior, and permits HAVING to refer to columns in the SELECT list and columns in outer subqueries as well.

您仅限于已检索的列。我不知道背后是否有技术原因,但这就是 SQL 标准的要求。

关于mysql - Column Unkown With UPPER in HAVING clause for Column alias,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15482637/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com