我有一个查询,我根据这样的数据创建一个字段:
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'
它必须是表中的一个字段,否则它不起作用?
答案 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