MySQL字段无法识别

时间:2016-07-13 12:43:37

标签: mysql

我有一个查询,我根据这样的数据创建一个字段:

SELECT 

id as ID, 
name as NAME, 
percent as PERCENT, 
hits AS HITS,
(30 * PERCENT) / 100 AS MAX_HITS_ALLOWED

FROM `table` 

WHERE MAX_HITS_ALLOWED > 10

我收到以下错误:

  

'where子句'

中的未知列'MAX_HITS_ALLOWED'

它必须是表中的一个字段,否则它不起作用?

2 个答案:

答案 0 :(得分:3)

MAX_HITS_ALLOWED是列别名,不能以这种方式使用。而是使用WHERE子句中的表达式。

SELECT 
    id as ID, 
    name as NAME, 
    percent as PERCENT, 
    hits AS HITS,
    (30 * PERCENT) / 100 AS MAX_HITS_ALLOWED
FROM `table` 
WHERE (30 * PERCENT) / 100 > 10

答案 1 :(得分:1)

您无法在where中使用别名。 请改用having子句:

SELECT 

id as ID, 
name as NAME, 
percent as PERCENT, 
hits AS HITS,
(30 * PERCENT) / 100 AS MAX_HITS_ALLOWED

FROM `table` 
having MAX_HITS_ALLOWED > 10