We have a database table that has keys and values where the value can be anything in a varchar field. The values are for attributes about an item and one attribute is price.
To make a select field for prices, I was using the Max()
function to find the greatest value in the value column which seemed to work.
Then when we got prices over £100, they started to not get returned as the max value. I under stand that this is because it is a string value and not numeric.
The confusion comes when running a command like select max(value) from attributes where value > 100
because now the statement recognises that 101 is > 100 but 99 is not so returns 101 as the max value, however without the where value > 100
clause, 99 is treated as > 101. Why does the > 100 clause work as a numeric comparison but max does not?
Is there a reason that this happens?
Do not mixed varchar with numeric,
idea solution is only stored numeric for used of max
aggregate function,
alternatively
max( cast(value as unsigned) )
When you are doing a MAX
, is a cast to string.
When you are doing comparison, is a cast to numeric.
Reason?
http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html#function_max
(it returns the maximum string value)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With