CodeIgniter Forums
Ci4 orderBy Float Value - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30)
+--- Thread: Ci4 orderBy Float Value (/showthread.php?tid=79951)



Ci4 orderBy Float Value - hieuhiti - 08-22-2021

Hi All,
I have a little problem with sort values (Float type) from database mysql.
I use -> orderBy("FieldFloat","desc"), 
But incorrect result, Please helpme


RE: Ci4 orderBy Float Value - ikesela - 08-22-2021

can you give more sample, since i have no problem with it.


RE: Ci4 orderBy Float Value - nfaiz - 08-22-2021

(08-22-2021, 07:27 AM)hieuhiti Wrote: Hi All,
I have a little problem with sort values (Float type) from database mysql.
I use -> orderBy("FieldFloat","desc"), 
But incorrect result, Please helpme

Check your datatype.

or you can try
PHP Code:
$builder->select('(FieldFloat * 1) AS FieldFloat, Field2');
$builder->orderBy('FieldFloat','desc'); 



RE: Ci4 orderBy Float Value - InsiteFX - 08-23-2021

Please read this:
MySQL - B.3.4.8 Problems with Floating-Point Values

Your problem may be a MySQL problem with floats.


RE: Ci4 orderBy Float Value - hieuhiti - 08-23-2021

Thanks for everyone's help,
I tried:
$this->orderBy('CAST(FieldFloat as DECIMAL(10,5))','DESC',FALSE);
and got the desired result.