Hi, I have mysql DB with product size with char type. ex. 1 oz, 2 oz, 16 oz, 28 oz. If I use "....order by proudctsize" then it show 1 oz, 16 oz, 2 oz, 28 oz in order. What should I do if I want them to show 1 oz, 2 oz, 16 oz, 28 oz in order since 2 < 16 so 2 should appear before 16? Any help is appreciated.
Yes change from varchar to int, cause if everything is oz there is no need to store this. Another way around this would be to sort it aftre the results are returned with some php functions
You could do something like this, but switching to an int or other numeric field would be better. ORDER BY CAST(proudctsize AS UNSIGNED)
Thank you very very much, jestep. It works. Now I have a situation. 1 oz, 5/8 oz. The result shows 1 oz first. Is there anywhere to have 5/8 oz shows up first since it is smaller than 1 oz? Another situation is having 1 oz and 1 gallon. The result shows 1 gallon first. Is there anywhere to have 1 oz shows up first? Thanks for any suggestion.
In this case I dont think there's any way to sort without having the unit in a separate column. Additionally there's no way to get around the fraction unless you can convert it into a decimal. Last option would be to make a relative volume column, where you would put a converted amount. You may be able to use some sort or trigger to do this automatically.
$result = mysql_query("SELECT round(5/8,2) "); result --> Array ( [0] => 0.63 [round(5/8,2)] => 0.63 ) $result = mysql_query("SELECT round('5/8',2) "); // 5/8 is a string result --> Array ( [0] => 5.00 [round('5/8',2)] => 5.00 ) Is there anyway to use the 2nd query but to get 0.63? Thanks for any help in advance.