Php MySQL>;汇总为单个查询(包括谷歌地图)

Php MySQL>;汇总为单个查询(包括谷歌地图),php,mysql,google-maps-api-3,Php,Mysql,Google Maps Api 3,这个Q是@Vincent Savard对我的一个Q的评论的后续 目前,我正在尝试(对于google maps XML请求,在60秒后超时)将数据从一个(相当)大的表拆分为许多较小的表,并在运行中转换/修改/等等。对于此任务,我将使用php来完成以下示例: // The following happens inside some functions. // The main table has some "groups" of content. // fn_a creates the new sm

这个Q是@Vincent Savard对我的一个Q的评论的后续

目前,我正在尝试(对于google maps XML请求,在60秒后超时)将数据从一个(相当)大的表拆分为许多较小的表,并在运行中转换/修改/等等。对于此任务,我将使用php来完成以下示例:

// The following happens inside some functions.
// The main table has some "groups" of content.
// fn_a creates the new small tables
"
CREATE TABLE {$small_table_a}
    col_group_a_id int UNSIGNED NOT NULL AUTO_INCREMENT,
    col_group_a_fname tinytext,
    col_group_a_lname tinytext,
    col_group_a_valA tinytext,
    col_group_a_valB tinytext,
    col_group_a_address tinytext,
    col_group_a_lat decimal(9,3),
    col_group_a_lng decimal(9,3),
    PRIMARY KEY  (id)
"
"
CREATE TABLE {$small_table_b}
    col_group_b_id int UNSIGNED NOT NULL AUTO_INCREMENT,
    col_group_b_fname tinytext,
    col_group_b_lname tinytext,
    col_group_b_valA tinytext,
    col_group_b_valB tinytext,
    col_group_b_address tinytext,
    col_group_b_lat decimal(9,3),
    col_group_b_lng decimal(9,3),
    PRIMARY KEY  (id)
"

// fn_b loads the content from the big table, modifies it and saves it row per row into the small tables
$sql = "
SELECT *
FROM {$big_table}
"
foreach ( $sql as $data )
{
    $id = $data->id;

    $group_a_fname = $data->group_a_fname;
    $group_a_lname = $data->group_a_lname;
    $group_a_lname = "{$group_a_fname}, {$group_a_lname}";
    $group_a_valA = $data->group_a_valA ? $data->group_a_valA : '-';
    $group_a_valA = $data->group_a_valB ? $data->group_a_valB : 'none';
    $group_a_valA = $data->group_a_address;

    $group_b_fname = $data->group_b_fname;
    $group_b_lname = $data->group_b_lname;
    $group_b_name = "{$group_b_fname}, {$group_b_lname}";
    $group_b_valA = $data->group_b_valA ? $data->group_b_valA : '/';
    $group_b_valA = $data->group_b_valB ? "€ {$data->group_b_valB}" : null;

    "
    INSERT INTO {$small_table_a} ... VALUES ...
    "
}

// fn_c pulls in data from the small tables, asks the google map API for lat & lng and _should_ update the small table

$sql = "
SELECT *
FROM {$small_table_a}
"
foreach ( $sql as $data )
{
    $output['id'] = $data->id;

    $address = urlencode( $data->address );
    $url = "http://maps.google.com/maps/api/geocode/xml?address={$address}&sensor=false";
    $content = file_get_contents( $url );
    $file_data = new SimpleXMLElement( $content );
    $file_data = $file_data->result ? $file_data->result : null;
    if ( ! $file_data ) 
        continue;
    $location = $file_data->geometry->location;
    $output['lat'] = (string) $location->lat;
    $output['lng'] = (string) $location->lng;
}
foreach ( $output as $data )
{
    "
    UPDATE {$table}
    SET lat=SET lat={$data['lat']}, lng={$data['lng']}
    WHERE id=$data['id']
}
问题:我如何在一个查询中做到这一点?或者如何减少数据库查询?当今天超过了我的地理编码限制时,我如何在不中断查询构建的情况下将lat/lng添加到表中?我不想因为超出了限制而放弃所有内容

谢谢


注意:这个例子是我直接用手写的。失败可能就在那里。

我们需要知道foreach循环中的INSERT-INTO查询是什么,因为这是一个可以汇总到一个查询中的查询。基本上,这是一个想法:

INSERT INTO {$small_table} -- you can specify which columns to fill,
                           -- i.e. INSERT INTO table (col_a, col_b)
    SELECT group_a_fname, group_a_lname, 
           group_a_valA, group_a_valB, 
           group_a_address, group_b_fname, 
           group_b_lname, group_b_valA, group_b_valB -- etc
    FROM {$big_table};
显然,您必须调整查询以满足您的需求。您只需要掌握其背后的思想:您可以使用SELECT查询插入行


更新查询不同,因为您必须依赖外部数据(网站)。我不认为有一个简单的方法可以在一个查询中完成,但我可能错了。

谢谢您的光临:)。问题是我需要动态修改这些值。类似于Q中显示的代码中的第一个
foreach
语句。@kaiser:你可以,看看CONCAT函数。这样我可以组合多个列,但不改变值,对吗?@kaiser:是的,你仍然可以改变值,这取决于你想做什么!我不知道你想在这里实现什么。那么不,我认为用你的RDBMS(因此,在一个查询中)实现它是不可能的。不过,您可能需要检查存储过程。通常,如果只使用存储在数据库中的数据,则可以进行任何更改。