我Order by
用于记录排序。我想在每次排序的最后一个位置选择 NULL 记录。例如。
ASC - {1,2,3,4,5,6,7,NULL,NULL,NULL}
DESC - {7,6,5,4,3,2,1,NULL,NULL,NULL}
如何在 Oracle 查询中执行此操作?
您的 order by 子句应如下所示:
描述
order by <field_name> desc nulls last;
升序
order by <field_name> asc nulls last;
基本 MySql 查询和基于 Yii2 的代码:
Basic Query in MySql:
SELECT *, IF(cloumn_name IS NULL, 1, 0) as cloumn_name_alias FROM `table_name` WHERE 1 ORDER BY cloumn_name_alias, cloumn_name ASC;
For Yii2 Framework:
$dataProvider = new ActiveDataProvider([
'query' => $query,
'sort' => [
'defaultOrder' => [
'position_alias' => SORT_ASC,
'position' => SORT_ASC,
],
'attributes' => [
'position_alias',
'position',
]
]
]);
升压 -
ORDER BY NVL(field, 10000000); <-- a maximum big constant here
DESC -
ORDER BY NVL(field, 0) DESC;