mysql - 按自定义值排序 Yii2

标签 mysql sql-order-by yii2

我想在自定义字段上对记录进行排序,但它在 yii2 中给我错误,任何人都可以帮助我。

->orderBy(player.PlayerRole, 'G','D','M','F'); 其中 G、D、M 和 F 是自定义值。这在 mysql 中工作正常,但在 yii2 中却不行。

这个我也试过

$expression = new Expression('field(player.PlayerRole,G,D,M,F)'); 

->orderBy($expression)

这里是完整的查询

$expression = new Expression('field(player.PlayerRole,G,D,M,F)');
return (new Query())
->select('*')
->from('tablename')`enter code here`
->orderBy(player.PlayerRole, 'G','D','M','F');
->all();

最佳答案

如本期所述:

Please read the doc carefully. orderBy takes an array, and an array element can be an Expression. So you should use $query->orderBy(array($expression)) instead of $query->orderBy($expression).

https://github.com/yiisoft/yii2/issues/553

所以试试 ->orderBy(array($expression))

关于mysql - 按自定义值排序 Yii2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32986132/

相关文章:

php - 如何将多对多关系查询结果放入yii2中的 GridView 中?

java - 如何使用 Java 故障转移到 AWS RDS MySQL 副本

sql - 为什么添加 RAND() 会导致 MySQL 过载?

mysql - 从 Laravel 中的多个表中排序

database - Postgresql - 使用特殊字符、数字和字母对字符字段进行排序

php - Yii2 人类可理解的 URL

php - 如果表格有错误。如何创建错误警报? [PHP、XHTML]

java - hibernate - 无法找到具有 ID 的实体

MySQL 查询仅选择一个条目

sorting - Yii2 过滤具有多重关系的数据