【问题标题】:PHP MySQL joins and outputting as arrayPHP MySQL加入并输出为数组
【发布时间】:2012-07-06 20:57:57
【问题描述】:

我想知道是否有人可以帮助我。

我有以下两个表格,位置和多边形。

positions:
----------------------------------------------
| positionID | name        | information
----------------------------------------------
| 1          | Southampton | text here
| 2          | London      | text here

.

polygons:
-------------------------------------------------------------------
| polygonID | positionID | lat           | lon
-------------------------------------------------------------------
| 1         | 1          | 434343.034343 | -43.4343434
| 2         | 1          | 343343.034343 | -43.4343434
| 3         | 1          | 434343.034343 | -54.4343434

我想把两张表连接起来,输出成数组,格式如下。

$positions = array(
               array( positionID => 1, 
                      name => 'Southampton',
                      information => 'text here',
                      polys =>  array( array( Lat => 54.54299483853406, 
                                      Lng => -6.5224456787109375,
                                     ),
                                array( Lat => 54.648809788121866, 
                                       Lng => -6.405029296875,
                                     ),
                                array( Lat => 54.54020652089137, 
                                       Lng => -6.39129638671875,
                                     ),
                                ),
               ),
       );

我已经编写了以下加入语句,但是我真的被困在下一步了。

SELECT * FROM (`positions`) LEFT JOIN `polygons` ON `positions`.`positionID` = `positions`.`positions`

连接中的纬度/经度数据必须存储在数组中的“polys”键中。

任何帮助或建议将不胜感激。

谢谢一百万

【问题讨论】:

    标签: php mysql arrays join


    【解决方案1】:

    如果每个位置ID 有多个多边形ID,您将获取大量位置副本。 我建议改为执行 2 个查询:

    $positions = array();
    
    $query_1 = "SELECT * FROM positions";
    $query_2 = "SELECT * FROM polygons";
    
    $resource_1 = mysql_query($query_1);
    
    while($position = mysql_fetch_assoc($resource_1))
    {
        $positions[$position['positionID']] = $position;
        $positions[$position['positionID']]['polys'] = array();
    }
    
    $resource_2 = mysql_query($query_2);
    
    while($polygon = mysql_fetch_assoc($resource_2))
    {
        $positions[$polygon['positionID']]['polys'][$polygon['polygonID']]['Lat'] = $polygon['lat'];
        $positions[$polygon['positionID']]['polys'][$polygon['polygonID']]['Lon'] = $polygon['lon'];
    }
    

    (抱歉在我的示例中使用了不推荐使用的函数,例如 mysql_query)

    【讨论】:

    • 谢谢?我想知道对于这类事情,一个查询还是两个查询哪个更有效?
    • GrammarNazizm: deprickated,depreciated,etc -> deprecated :)
    【解决方案2】:

    您只需要一个查询,但您仍然需要在 PHP 中对结果进行一些处理:

    $sql = "SELECT * FROM positions
        LEFT JOIN polygons ON positions.positionID = polygons.positionID";
    $result = mysql_query($sql);
    if (mysql_num_rows($result) > 0) {
        $positions = array();
        while ($row = mysql_fetch_assoc($result)) {
            if (!isset($positions[$row['name']])) {
                $positions[$row['name']] = array(
                    'positionID' => $row['positionID'],
                    'information' => $row['information'],
                    'polys' => array()
                    );
            }
            $positions[$row['name']]['polys'][] = array(
                'lat' => $row['lat'],
                'lon' => $row['lon']
                );
        }
    
        print_r($positions);
    }
    

    这也将返回实际上没有任何多边形信息的位置。为了防止这种情况,只需将LEFT JOIN 更改为JOIN

    至于哪个效率最高,恐怕我不知道答案。如果可以的话,最好的办法是对这种方法和 Puggan Se 建议的方法进行基准测试。

    【讨论】:

    • 太棒了。效果很好。 :) 谢谢一百万
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-03-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多