+ 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??
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.
+ 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)
+ 1
If there is a nicer way please tell :)
+ 1
I defined in mysql Modul as I NT shouldn't be possible to put the sth else or?
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;}