【发布时间】:2018-03-08 14:46:25
【问题描述】:
我有一个计划应用程序表,我正在尝试将最近的 5 个应用程序返回到一个属性,通过 Postgres 查询通过返回为 JSON 数组的 PHP 按最近到最远距离排序。
结果已成功返回,但它们的排序似乎不正确。起初我以为是因为 ST_Distance 的结果值是浮点数,所以将它们转换为整数,但它们仍然没有按距离返回记录。
这个 SQL 有什么明显的错误吗?
// Search the database for all similar items
$sql = pg_query($conn, "SELECT DISTINCT b.reference, b.application_number, b.site_address, b.proposal, cast((st_distance(a.geom, b.geom)) as integer) as dist FROM addresses.llpg_standard a, planning.planning_applications_current b WHERE a.uprn = $query ORDER BY dist ASC LIMIT 5");
$array = array();
我的查询结果如下,你可以看到'dist'值没有排序...
[
{
"reference": "228028",
"application_number": "RU.17\/1320",
"site_address": "64 The Avenue\r\nEgham\r\nTW20 9AD",
"proposal": "Application seeking approval of details pursuant to condiitions 2 (Materials), 3 (Surfacing Materials), 4 (Tree Portection), 10 (Construction Transport Management Plan), 12 (Travel Plan), 13 (Construction Management Plan), 21 (Gas Prroof Membrane), 23 (Archaeology), (24( Flood Risk Management Plan), 28 (CEMP) of planning permission RU.16\/1453 (80 bed Care Home, Ancillary facilities and retention of 64 The Avenue).",
"dist": "120"
},
{
"reference": "228568",
"application_number": "RU.17\/1303",
"site_address": "64 The Avenue\r\nEGHAM\r\nTW20 9AD",
"proposal": "Proposed three storey 80 bed care home with ancillary facilities in the roof space.",
"dist": "120"
},
{
"reference": "233449",
"application_number": "RU.17\/1820",
"site_address": "Egham Leisure Centre\r\nVicarage Road\r\nEGHAM\r\nTW20 8NL",
"proposal": "Variation of conditions 2 (approved plans), 5 ( Sustainable Drainage Plan) and 29 ( Flood Risk Assessment) of RU.17\/0488",
"dist": "280"
},
{
"reference": "236908",
"application_number": "RU.18\/0089",
"site_address": "Egham Leisure Centre\r\nVicarage Road\r\nEGHAM\r\nTW20 8NL",
"proposal": "Details pursuant to Condition 28 (Replacement Temporary Bin Storage) of planning permission RU.17\/0488 (Demolition of existing leisure centre and erection of replacement leisure centre (Use Class D2); with 1no. outdoor synthetic sports pitch with associated fencing and lighting columns; new service access off Vicarage Road; alterations to the existing car park including overflow area; landscaping and public realm works; installation of a substation; and associated works). ",
"dist": "280"
},
{
"reference": "239748",
"application_number": "RU.18\/0388",
"site_address": "1 Vicarage Crescent\r\nEGHAM\r\nTW20 9JP",
"proposal": "Proposed lawful development Certificate to establish whether planning permission is required for a rear extension",
"dist": "18"
}
]
【问题讨论】:
-
可以分享一下查询结果吗?
-
@JGH 我已经添加了查询结果。
标签: sql postgresql casting sql-order-by postgis