我得到的SQL值如下:
0: {val_point: "1"}
1: {val_point: "10"}
2: {val_point: "11"}
3: {val_point: "12"}
4: {val_point: "13"}
5: {val_point: "14"}
6: {val_point: "15"}
7: {val_point: "16"}
8: {val_point: "17"}
9: {val_point: "18"}
10: {val_point: "19"}
11: {val_point: "2"}
12: {val_point: "20"}
13: {val_point: "21"}
我喜欢它们的实际数字顺序(即1,2,3,4 ......)。此列中的值在mySQL中存储为字符串,我不能(出于各种原因)将数据类型更改为INT。
我试过这个并得到了上面的结果:
$sql = "select distinct `{$whereCategory}` as val_point from {$table} ORDER BY `{$whereCategory}` ASC;";
$result = $dbh->query($sql)->fetchAll(PDO::FETCH_ASSOC);
$final = natcasesort($result);
header('Content-type: application/json');
echo json_encode($final);
答案 0 :(得分:2)
试试这个
$sql = "select distinct `{$whereCategory}` as val_point,CONVERT({$whereCategory},UNSIGNED INTEGER) AS cat from {$table} ORDER BY cat ASC";