ThinkPHP5 Mysql Varchar字符串排序

2022-5-25 / 0 评论 / 111 阅读

1.实现

ThinkPHP5 Mysql Varchar字符串排序
百度各种搜索最终三个方法:
ORDER BY name+0 desc
ORDER BY CAST(name as SIGNED) desc
ORDER BY CONVERT(name as SIGNED) desc

但是经过实践不知道为什么排序出来的结果只有倒序,没有正续

2.另一个

经过Google后。。
第二个实现方法:
ORDER BY LPAD(LOWER(name), 10,0) DESC

原文地址:https://stackoverflow.com/questions/4686849/sorting-varchar-field-numerically-in-mysql

3. PHP

$order2 = "$order $desc";
if(in_array($order, ['name', 'ip']))
{
    $order2 = "LPAD(LOWER($order), 10,0) $desc";
}

Db::name('xxxx')->orderRaw($order2)->paginate(50, false,['query'=>request()->param()])

评论一下?

OωO
取消