给我的号码下订单 natsort();效果不佳

Giving an order to my numbers natsort(); doesn't work well

我在从我的数据库加载内容时遇到问题。 在我的数据库中,我有一个 table 用于保存我的画廊图片。 在我的行中,标题被设置为文本,因为我想使用的值是:hashtag,然后是像这样的数字 #1 #2 ...等等。

所以我正在这样加载我的内容...

$sql = "SELECT * FROM gallery ORDER BY title DESC LIMIT $start_from, $per_page";
$run = mysqli_query($conn,$sql);

        while ($rows = mysqli_fetch_assoc($run))
        {  

            echo'....... ';}

但问题是,当我按标题订购时,我得到这些数字

hastag 90

hastag 9-2

hastag 9-1

所以我使用的是 ORDER BY title DESC 为什么我的最大数字(标签 706)不是第一个?

我尝试使用 natsort(); 但它没有用,因为它保留了同样的东西。

我该如何解决这个问题?

9-2 不是数字。它是 STRING,这意味着您的 title 字段是文本类型(char、varchar),并且适用 STRING 排序规则。这意味着比较字符串 "character-by-character",而不是 "number-by-number".

9-2
7
abc

comparison a) "9" > "7" -> true
comparison b) "-" > "" -> true
comparison c) "2" > "" -> true

类似地:

9-2
706
abc

comparison a) "9" > "7" -> true   (ascii 57 > ascii 55)
comparison b) "-" > "0" -> false  (ascii 45 > ascii 48)
comparison c) not necessary, 9-2 is already "bigger" than 706

所以 9-2 是一个比 706 更大的字符串,因为 9>7 总是正确的。