我使用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 ); }
这是我使用的获取功能 &安培;我在我的代码中使用它如下。
$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 );
}
}
我想按正确顺序对其进行排序,如果属性没有构建,则只按单元号对其进行排序。欢迎任何帮助解决此问题。感谢。
答案 0 :(得分:0)
在这种情况下,您需要查看字符串并了解如何处理它们。它看起来像是一个“建筑X - Y”形式的字符串,你想要对X和Y进行排序。简单的做法是将其转换为数字数组。您可以通过以下方式执行此操作:
.....
ORDER BY string_to_array(regexp_replace(building_name, 'Building ', ''), ' - ')::int[]
这会将“建筑物X - Y”变为{X,Y},因此建筑物1 - 100变为{1,100},依此类推。这些将从最左边的元素开始排序。