Page is a not externally linkable
- Code, Content, and Presentation
-- Databases
---- Ordering column with negative and positive decimal numbers in MySQL


rocknbil - 4:08 pm on May 31, 2012 (gmt 0)


Are the commas in your example typos?

Are your field types decimal? By the look of it, it's not, and you'll get those results if it's a textual field.

You don't need a + by positive values.

I hope your field name is not "column," column is a reserved word and you'll have to use backticks (NOT quotes) to access it.

select `column` from `tablename` order by `column` asc;

create table deci_test (decivalues decimal(12,2) not null default '0.00');

insert into deci_test (decivalues) values ('-20.00');
insert into deci_test (decivalues) values ('0.5');
insert into deci_test (decivalues) values ('-19.00');
insert into deci_test (decivalues) values ('1.0');
insert into deci_test (decivalues) values ('-18.50');
insert into deci_test (decivalues) values ('-19.50');
insert into deci_test (decivalues) values ('-0.7');
insert into deci_test (decivalues) values ('0.0');

select decivalues from deci_test order by decivalues asc;
+------------+
| decivalues |
+------------+
| -20.00 |
| -19.50 |
| -19.00 |
| -18.50 |
| -0.70 |
| 0.00 |
| 0.50 |
| 1.00 |
+------------+
8 rows in set (0.02 sec)

If they're not decimal, make them so.

alter table yourtable change yourfield yourfield decimal (12,2) not null default '0.00';


Thread source:: http://www.webmasterworld.com/databases_sql_mysql/4459880.htm
Brought to you by WebmasterWorld: http://www.webmasterworld.com