0

我一直在尝试按照此页面上的解决方案在 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

4

1 回答 1

0

不是 PHP 人员或 Zend 人员,但我查看的示例会写得更像这样(注意你在原始 order() 子句中重复了 column_a )。

$sql1 = $db->select()
  ->distinct()
  ->from('table1', 'column_a')
  ->where('someVal LIKE ?', 'X%');

$sql2 = $db->select()
  ->distinct()
  ->from('table1', 'column_b')
  ->where('someVal LIKE ?', 'X%');

    $union = $db->select()
   ->union(array($sql1, $sql2))
   // 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_b), '[0-9]+$')) DESC"),
        'column_a DESC'
   ));
于 2014-06-02T19:03:59.067 回答