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'
   ));

これを行うと、エラーが発生しますORA-01785: ORDER BY item must be the number of a SELECT-list expression。これは、union(?) の結果として column_a と column_b の両方が column_a になり、名前ではなく番号で列を参照する必要があるためだと思いますが、new Zend_Db_Expr()(つまり、の順序で動作しcolumn_a DESCます)。

EDITto_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 に答える