So I have a varchar column which is supposed to store product prices( don't ask me how I ended up with that, but now I have no option of changing this...sigh ), It can also be blank, or contain one of the texts(literally) "null","OUT" where both represents a price of 0. What is the best and most efficient way to find the MIN and MAX value of this column?
PS: I am open to php/mysql hybrid solutions, cause I need the most optimized and efficient way for this. 开发者_运维百科This table is really huge...
Something like this should work and be reasonably efficient. It's not going to be fast in any case, though.
SELECT
MIN(CAST(price AS DECIMAL(8,2))) as price_min,
MAX(CAST(price AS DECIMAL(8,2))) as price_max
FROM products
WHERE price NOT IN('', 'null', 'OUT')
After testing this, I noticed that apparently the casting is done automatically, so you can just do:
SELECT
MIN(price) as price_min,
MAX(price) as price_max
FROM products
WHERE price NOT IN('', 'null', 'OUT')
精彩评论