1

我的代码使用 php 5.3。我想按以下格式对我的数据进行排序。

01 - 101
号楼 01 - 150 号楼

02 号楼 - 100
号楼 02 号楼 - 105
号楼 03 号楼 - 099 号楼

public static function fetchSortedPropertyUnits() {
    $strSql = 'SELECT pu.*,pb.building_name
               FROM property_units pu  
                LEFT JOIN property_buildings pb ON( pu.property_building_id = pb.id )  
                WHERE pu.management_company_id = ' . $intManagementCompanyId . '
                    AND pu.property_id = ' . $intPropertyId . '  
               ORDER BY  
                COALESCE ( CAST ( SUBSTRING ( pb.building_name FROM \'([a-zA-Z ]{1,26})\' ) AS VARCHAR ), \'\' ),
                    COALESCE ( CAST ( SUBSTRING ( pb.building_name FROM \'([0-9]{1,10})\' ) AS INTEGER ), 0 ),
                    COALESCE ( CAST ( SUBSTRING ( pu.unit_number FROM \'([a-zA-Z ]{1,26})\' ) AS VARCHAR ), \'\' ),
                    COALESCE ( CAST ( SUBSTRING ( pu.unit_number FROM \'([0-9]{1,10})\' ) AS INTEGER ), 0 ),
                    pb.building_name,
                    pu.unit_number';  
            return self::fetchPropertyUnits( $strSql, $objDatabase );  }

这是我使用的 fetch 功能。
&我在我的代码中使用它如下。

$arrobjSortedPropertyUnits  =   CPropertyUnits::fetchSortedPropertyUnits( $this->m_objPropertyUtilitySetting->getManagementCompanyId(), $this->m_objPropertyUtilitySetting->getPropertyId(), $this->m_objClientDatabase );  
foreach( $this->m_arrobjPropertyUnits as $objPropertyUnit ) {  
    $strUnitNumber = $objPropertyUnit->getUnitNumber();  
    if( true == valObj( $objPropertyUnit, 'CPropertyUnit' ) && true == $objPropertyUnit->getPropertyBuildingId() ) {  
        $strUnitNumber = $objPropertyUnit->getBuildingName() . ' - ' . $objPropertyUnit->getUnitNumber();  
        $objPropertyUnit->setUnitNumber( $strUnitNumber );  
    }  
}  

我想按正确的顺序对其进行排序,如果财产没有建筑物,则仅按单元编号对其进行排序。欢迎对此问题提供任何帮助。谢谢。

4

1 回答 1

0

在这种情况下,您需要查看您的字符串并了解如何处理它们。看起来有一个“Building X - Y”形式的字符串,并且您想先按 X 排序,然后按 Y。简单的做法是将其转换为数字数组。您可以通过以下方式做到这一点:

 .....
 ORDER BY string_to_array(regexp_replace(building_name, 'Building ', ''), ' - ')::int[]

这会将“Building X - Y”变为 {X,Y},因此 Building 1 - 100 变为 {1,100},依此类推。这些将从最左边的元素开始排序。

于 2013-06-07T07:36:10.180 回答