I have an alias in the select like this:
SELECT
CASE ....
WHEN 1 THEN somefield * -10
WHEN 0 THEN somefield * 10
END as myAlias
FROM
....
the problem is that at the first record that mysql seeks, it marks the “myAlias” as UNSIGNED
but in some next record the same alias produces a SIGNED integer, then I have the error:
BIGINT UNSIGNED value is out of range in....
so, how to tell mysql before it does any operation that “myAlias” is a SIGNED INTEGER?
thanks!
edit:
I’ve tried
SELECT
CAST(CASE ....
WHEN 1 THEN somefield * -10
WHEN 0 THEN somefield * 10
END AS SIGNED) as myAlias
FROM
....
without success!
I assume that the field in your table is declared as
UNSIGNED INTEGER. In that case, you have to type cast the field in theTHENclause of theCASEexpression. Below example illustrates a sample.Create and insert script:
Type casting the field in THEN clause: SQL Fiddle Demo
Output:
Alternative way to write the above query: SQL Fiddle Demo
Output:
Without type casting the field: SQL Fiddle Demo
Error: