列在条件上的Mysql

时间:2013-06-20 08:46:47

标签: mysql

最小的例子

select a.order_id as X from orders as a
WHERE
X > 8000

查询将失败,因为X不是列,任何解决方案?

其他例子

select (if (E.size > 0, E.Size, (B.height x B.width)) as sizeX from
orders as a, report as E, size as B

where

(E.id = a.id and B.id = a.id)
and
sizeX > 100

我的查询可能包含拼写错误,但我希望我的问题很明确

2 个答案:

答案 0 :(得分:3)

您有两个选择:

1)使用HAVING(效率不是很高,因为在返回结果后应用了having子句的条件,因此没有使用索引)

select a.order_id as X from orders as a
HAVING
X > 8000

2)使用列名称(如果索引是where子句中使用的列,则效率很高)

select a.order_id as X from orders as a
WHERE
a.order_id > 8000

答案 1 :(得分:2)

标准SQL不允许在WHERE子句中引用列别名,请参阅文档here