thinkphp 5.1 thinkphp5 使用中文字符排序

浏览:3370 发布日期:2019/03/05 分类:技术分享 关键字: 排序 中文字符排序 utf8编码
环境:版本:tp 5 或者tp 5.1
MySQL编码:utf-8
目的:实现中文首字母的拼音排序
thinPHP5 使用order排序,但实际操作中发现,使用order对中文排序无法做到按中文首字母的拼音排序。
通过查询MySQL对中文编码的sql语句,得到如下的命令:
select * from tableName CONVERT( FieldName USING gbk ) asc

那么如何实现匹配到thinkPHP 5中呢?
测试:
order('CONVERT( FieldName USING gbk )','asc') #报错
order('CONVERT( FieldName USING gbk ) asc') #报错

通过查询thinkPHP源代码,发现了orderRaw方法,其代码如下:
/**
* 表达式方式指定Field排序
* @access public
* @param string $field 排序字段
* @param array $bind 参数绑定
* @return $this
*/
public function orderRaw($field, $bind = [])
{
if ($bind) {
$this->bindParams($field, $bind);
}

$this->options['order'][] = $this->raw($field);

return $this;
}

通过使用orderRaw('CONVERT( FieldName USING gbk )','asc') 成功得到目标数据排序。
最佳答案
评论( 相关
后面还有条评论,点击查看>>