#1054 - 'where子句'中的未知列'general'

时间:2013-06-07 17:10:25

标签: mysql sql

我有一个我不明白的问题;为什么不能使用该列?这是我的代码:

SELECT `a`.`id`, `a`.`title`, `e`.`url` as category, 
  calc_val(`a`.`value`, `c`.`prof`, `c`.`tax`) as general, 
  `a`.`date`, `a`.`url`, 
  SUM(`d`.`stock`) as stock 
FROM (`produc` as a) 
LEFT JOIN `prod_c` as b ON `a`.`id` = `b`.`p_id` 
LEFT JOIN `p_calc` as c ON `a`.`id` = `c`.`p_id` 
LEFT JOIN `p_opt` as d ON `a`.`id` = `d`.`p_id` 
LEFT JOIN `categ` as e ON `b`.`categ_id` = `e`.`id` 
LEFT JOIN `prod_aval` as `f` ON `a`.`id` = `f`.`p_id` 
WHERE `b`.`categ_id` = '1' 
  AND `a`.`active` = 'y' 
  AND `d`.`stock` >= 1 
  AND general BETWEEN '10' AND '50' 
GROUP BY `a`.`id` 
LIMIT 9

calc_val = routine:

begin
    declare value real;
    set value = v+(v*l/100);
    return value+(value*i/100);
end

2 个答案:

答案 0 :(得分:2)

别名不能在WHERE子句中使用,因此您需要包含计算:

AND calc_val(`a`.`value`, `c`.`prof`, `c`.`tax`) BETWEEN '10' AND '50'

或者,您可以使用派生表并将部分或全部WHERE子句应用于它。如果计算成本高昂/复杂/处理器密集,这可能很有用。

SELECT * FROM (
    SELECT `a`.`id`, `a`.`title`, `e`.`url` as category, 
      calc_val(`a`.`value`, `c`.`prof`, `c`.`tax`) as general, 
      `a`.`date`, `a`.`url`, 
      SUM(`d`.`stock`) as stock 
    FROM (`produc` as a) 
    LEFT JOIN `prod_c` as b ON `a`.`id` = `b`.`p_id` 
    LEFT JOIN `p_calc` as c ON `a`.`id` = `c`.`p_id` 
    LEFT JOIN `p_opt` as d ON `a`.`id` = `d`.`p_id` 
    LEFT JOIN `categ` as e ON `b`.`categ_id` = `e`.`id` 
    LEFT JOIN `prod_aval` as `f` ON `a`.`id` = `f`.`p_id` 
    WHERE `b`.`categ_id` = '1' 
      AND `a`.`active` = 'y' 
      AND `d`.`stock` >= 1 
    GROUP BY `a`.`id` 
    LIMIT 9
)
WHERE general BETWEEN '10' AND '50'

答案 1 :(得分:0)

如果您使用MySql Server版本5.5+,则术语“GENERAL”是保留字。查看此页面以了解更多内容:http://dev.mysql.com/doc/refman/5.5/en/reserved-words.html#table-reserved-words-new-5.5

HTH,安迪