我一直在尝试按照此页面上的解决方案在 Oracle (11g2) 中实现自然排序,但我无法让它与 Zend_Db_Select (ZF1) 和联合查询一起使用。我的查询看起来像这样:
<?php
$sql[] = $db->select()
->distinct()
->from('table1', 'column_a')
->where('someVal LIKE ?', 'X%');
$sql[] = $db->select()
->distinct()
->from('table1', 'column_b')
->where('someVal LIKE ?', 'X%');
$union = $db->select()
->union(array($sql[0], $sql[1]))
// here is the part from the other page
->order(array(
new Zend_Db_Expr("to_number(regexp_substr(column_a, '^[0-9]+')) DESC"),
new Zend_Db_Expr("to_number(regexp_substr(column_a, '[0-9]+$')) DESC"),
'column_a DESC'
));
当我这样做时,我得到 error ORA-01785: ORDER BY item must be the number of a SELECT-list expression
。我猜这是因为由于联合(?),column_a 和column_b 都变成column_a 并且它希望我按数字而不是名称来引用列,但是如果我取出以开头的两行new Zend_Db_Expr()
(即它column_a DESC
按顺序使用)。
编辑:删除了to_number(regexp(substr(column_a, '^[0-9]+'))
最初关闭的右括号column_a