Aamir Mahmood
Aamir Mahmood

Reputation: 2724

Mysql where condition on columns

Minimal example

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

query will fail as X is not a column, any solution?

other example

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

my query may contain typos, but I hope my question is clear

Upvotes: 1

Views: 58

Answers (2)

Stephan
Stephan

Reputation: 8090

You have 2 options:

1) Use HAVING (not very efficient since the conditions from having clause are applied AFTER the results are returned and thus NO indexes are used)

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

2) Use the column name (efficient if you have a index an the column used in where clause)

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

Upvotes: 3

PbxMan
PbxMan

Reputation: 7625

Standard SQL disallows references to column aliases in a WHERE clause see the doc here

Upvotes: 2

Related Questions