Mysql Order by went Wrong. Why behave like char? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Mysql Order by went Wrong. Why behave like char?

Hi I have the Problem that i want to order my db table by : SELECT * FROM `videos` ORDER BY `Modul` ASC Modul is a integer typ but the result of the oder action shows that modul behaves like char dont know why??

28th Nov 2018, 1:58 PM
jack
jack - avatar
5 Answers
+ 6
I'm not even sure about this, and it may sound silly, but maybe try to remove the quotes/backtick off the Modul field name.
28th Nov 2018, 5:27 PM
Ipang
+ 1
Verify the column datatype is really not VARCHAR: DESCRIBE videos; maybe try to sort by directly converting the values to number: ORDER BY CAST(Modul AS UNSIGNED)
28th Nov 2018, 2:08 PM
Tibor Santa
Tibor Santa - avatar
+ 1
If there is a nicer way please tell :)
28th Nov 2018, 2:22 PM
jack
jack - avatar
+ 1
I defined in mysql Modul as I NT shouldn't be possible to put the sth else or?
28th Nov 2018, 2:24 PM
jack
jack - avatar
0
found out that the code $db_res= mysqli_query($db_link, "SELECT * FROM `videos` ORDER BY CAST(`Modul` AS INT)"); $array=array(); while($row=mysqli_fetch_array($db_res)){ array_push($array,$row); } and <script> var vidArray= <?php echo json_encode($array); ?>;</script> gives a string back what a previous integer was. In Javascript I'll cast the Modul back to int and sort them via the arr.sort(function(a,b){ a=parseInt(a); b=parseInt(b); if(a.Modul>b.Modul){return 1;} if(a.Modul<b.Modul){return -1;}
28th Nov 2018, 2:19 PM
jack
jack - avatar