Question

Example:

SELECT s.title, t.description,
u1.value * s.cache_value as Cache,
u2.value * s.drive_value as Drive,
u3.value * s.error_value as Error,
u3.value * s.error_value + u3.value * s.error_value as ErrorHigh,
u3.value * s.error_value - u3.value * s.error_value as ErrorLow,
m.area
FROM storage s
JOIN status t on t.id = s.status_id
JOIN manufac m on m.id = s.area
JOIN unit u1 on s.cache_unit_id = u1.id
JOIN unit u2 on s.drive_unit_id = u2.id
JOIN unit u3 on s.error_unit_id = u3.id

Desired goal:

SELECT s.title, t.description,
u1.value * s.cache_value as Cache,
u2.value * s.drive_value as Drive,
u3.value * s.error_value as Error,
u3.value * s.error_value + u3.value * s.error_value as ErrorHigh,
u3.value * s.error_value - u3.value * s.error_value as ErrorLow,
m.area
FROM storage s
JOIN status t on t.id = s.status_id
JOIN manufac m on m.id = s.area
JOIN unit u1 on s.cache_unit_id = u1.id
JOIN unit u2 on s.drive_unit_id = u2.id
JOIN unit u3 on s.error_unit_id = u3.id
WHERE 123 BETWEEN ErrorHigh AND ErrorLow;

MySQL returns syntax Error unknown column ErrorHigh

Note: ErrorHigh and 'ErrorLow` are the the new columns I create to display the computed values

Was it helpful?

Solution

The 1054 error is because SQL doesn't support referencing column aliases in the WHERE clause - the earliest MySQL supports is the GROUP BY. But most other databases only support column aliases in the ORDER BY clause.

If you want to continue to use the column alias, the operation it represents needs to be performed in a subselect, derived table/inline view. Otherwise, you can replace the column alias with the operation it represents - like this:

WHERE 123 BETWEEN u3.value * s.error_value + u3.value * s.error_value 
              AND u3.value * s.error_value - u3.value * s.error_value
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top