imgrooot Posted August 16, 2016 Share Posted August 16, 2016 I have a Mysql table where I store prices of a product. I have the values in "varchar" format because I needed to include "decimals" in the prices(for eg. 15.30) and "int" doesn't allow that. Having done that, when ever I filter the products by prices in Ascending or Descending order, they don't show up in proper order. It seems like the decimal in the prices is messing up the order. Is there a way to fix this so that the php query can filter the prices in proper order despite the decimals? Here's the eg of the query. $get_records = $db->prepare("SELECT * FROM records ORDER BY records.price DESC"); Quote Link to comment https://forums.phpfreaks.com/topic/301913-how-do-you-order-by-asc-and-desc-with-decimal-numbers-in-mysql-database/ Share on other sites More sharing options...
mac_gyver Posted August 16, 2016 Share Posted August 16, 2016 you would want to store the data in column defined as a decimal data type - http://dev.mysql.com/doc/refman/5.7/en/fixed-point-types.html Quote Link to comment https://forums.phpfreaks.com/topic/301913-how-do-you-order-by-asc-and-desc-with-decimal-numbers-in-mysql-database/#findComment-1536230 Share on other sites More sharing options...
imgrooot Posted August 16, 2016 Author Share Posted August 16, 2016 you would want to store the data in column defined as a decimal data type - http://dev.mysql.com/doc/refman/5.7/en/fixed-point-types.html There is a problem with that though. It removes any number after the decimal and instead adds .00 to it. Quote Link to comment https://forums.phpfreaks.com/topic/301913-how-do-you-order-by-asc-and-desc-with-decimal-numbers-in-mysql-database/#findComment-1536235 Share on other sites More sharing options...
Jacques1 Posted August 16, 2016 Share Posted August 16, 2016 Not with a proper ALTER TABLE statement. ALTER TABLE records MODIFY price DECIMAL(10, 2); Quote Link to comment https://forums.phpfreaks.com/topic/301913-how-do-you-order-by-asc-and-desc-with-decimal-numbers-in-mysql-database/#findComment-1536237 Share on other sites More sharing options...
Solution imgrooot Posted August 16, 2016 Author Solution Share Posted August 16, 2016 Not with a proper ALTER TABLE statement. ALTER TABLE records MODIFY price DECIMAL(10, 2); Yes you're right. Thanks a bunch. Quote Link to comment https://forums.phpfreaks.com/topic/301913-how-do-you-order-by-asc-and-desc-with-decimal-numbers-in-mysql-database/#findComment-1536238 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.