Mysql连接表数据,需要从1个查询中拖出数据

时间:2012-05-18 08:53:14

标签: php mysql xml

所以我基于mysql表中的数据生成XML文件,但需要能够使用我的查询从2个表中获取我想要的数据,并且不确定如何执行此操作。详情如下:

TABLE: store_locations
===================================
store_location_id
country
latitude
longitude
===================================

TABLE: store_locations_descriptions
===================================
store_location_id
name
description
city
===================================

PHP功能:

// Function to generate XML file based on store data from database
function fn_store_locator_generate_xml(){
    $qur = db_get_field("JOIN QUERY WANTED HERE");
    $ans=mysql_query($qur);
    $output.= "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<kml xmlns=\"http://earth.google.com/kml/2.2\">
<Document>";
    while($row=mysql_fetch_array($ans))
    {
      $output.="<name>".$row['name']."</name>";
      $output.="<description>".$row['description']."</surname>";
      $output.="<Placemark>";
      $output.="<name>".$row['name']."</name>";
      $output.="<Snippet>".$row['description']."</Snippet>";
      $output.="<description>".$row['description']."</description>";
      $output.="<Point>";
      $output.="<coordinates>".$row['latitude'].",".$row['longitude']."</coordinates>";
      $output.="</Point>";
      $output.="</Placemark>";
      $output.="</person>";
    } 
    $output.="</Document>";

    $file_name = "galleries.xml";
    $file_pointer = fopen($file_name, "w+");
    fwrite($file_pointer, "$output");
    fclose($file_pointer);
}
// generate the XML file
fn_store_locator_generate_xml();

非常感谢提前!

4 个答案:

答案 0 :(得分:1)

看起来你只需要加入。我假设你在这些表之间有一对一的关系。

select s.store_location_id,
    s.country,
    s.latitude,
    s.longitude,
    d.name,
    d.description,
    d.city
from store_locations s
    join store_locations_descriptions d
    on s.store_location_id = d.store_location_id

你当然应该研究联接是如何工作的: http://www.keithjbrown.co.uk/vworks/mysql/mysql_p5.php

答案 1 :(得分:0)

SELECT * FROM store_locations JOIN store_locations_descriptions,但你应该看一下mysql join教程,连接对于操纵数据库中的数据至关重要,例如:mysql join tutorial

答案 2 :(得分:0)

SELECT sl.country, sl.latitude, sl.longitude, sld.name, sld.description, sld.city
FROM store_locations sl
JOIN store_location_descriptions sld
ON sl.store_location_id=sld.store_location_id

答案 3 :(得分:0)

试试这个

select s.foo sd.foo
from store_locations s
join store_locations_descriptions sd
on s.store_location_id = sd.store_location_id