2011-11-08 117 views
0

我想知道是否有人可以幫助我。SQL加入問題

我試圖合併來自兩個mySQL數據庫表的信息時出現問題。

到目前爲止,我已經放在一起的查詢如下所示。

<?php 
require("phpfile.php"); 

// Start XML file, create parent node 

$dom = new DOMDocument("1.0"); 
$node = $dom->createElement("markers"); 
$parnode = $dom->appendChild($node); 

// Opens a connection to a MySQL server 

$connection=mysql_connect ("hostname", $username, $password); 
if (!$connection) { die('Not connected : ' . mysql_error());} 

// Set the active MySQL database 

$db_selected = mysql_select_db($database, $connection); 
if (!$db_selected) { 
die ('Can\'t use db : ' . mysql_error()); 
} 

$query = "SELECT findid, 
       findosgb36lat, 
       findosgb36lon, 
       findcategory, 
       findname, 
       finddescription 
      FROM finds 
      WHERE makepublic = 'Yes' 
      AND sites.sitetype, 
       sites.sitedescription, 
       sites.siteosgb36lat, 
       sites.osgb36lon"; 
$result = mysql_query($query); 
if (!$result) { 
die('Invalid query: ' . mysql_error()); 
} 

header("Content-type: text/xml"); 

// Iterate through the rows, adding XML nodes for each 

while ($row = @mysql_fetch_assoc($result)){ 
// ADD TO XML DOCUMENT NODE 
$node = $dom->createElement("marker"); 
$newnode = $parnode->appendChild($node); 
$newnode->setAttribute("findosgb36lat",$row['findosgb36lat']); 
$newnode->setAttribute("findosgb36lon",$row['findosgb36lon']); 
$newnode->setAttribute("findcategory",$row['findcategory']); 
$newnode->setAttribute("findname",$row['findname']); 
$newnode->setAttribute("finddescription",$row['finddescription']); 
} 

echo $dom->saveXML(); 

?> 

我的問題是,我不知道如何把從「點」表中的所有記錄,但只有那些記錄從「查找」表,其中「makepublic」值「是」 。我已經做了一些研究,看看是否有特定的連接,即左或右的工作,但由於表之間沒有共同的字段,我知道這些不起作用。

有人可能會告訴我如何解決這個問題,請。

非常感謝

+1

如果表格之間沒有共同的領域,你希望他們以什麼方式相互聯繫?你是否在尋找聯盟? – Jodaka

+0

聽起來更像一個[union](http://dev.mysql.com/doc/refman/5.0/en/union.html),而不是一個連接,但沒有一個共同的列,你如何預測數據對齊? (除非我不正確理解表格的結合) –

+1

每個表格的模式是什麼?如果它們相同或者您選擇了等效字段,則可以使用聯合。 –

回答

0

都非常感謝您對這個幫助。我今天一直在研究這個問題,並通過使用'UNION ALL'查詢解決了我所遇到的所有問題。親切的問候

0

製作有關表結構的某些假設:

 SELECT s.site_id, 
      s.sitetype, 
      s.sitedescription, 
      s.siteosgb36lat, 
      s.osgb36lon, 
      f.findid, 
      f.findosgb36lat, 
      f.findosgb36lon, 
      f.findcategory, 
      f.findname, 
      f.finddescription 
     FROM sites s 
     LEFT OUTER JOIN finds f on s.site_id = f.site_id and f.makepublic = 'Yes' 
+0

嗨,所有非常感謝回覆我的文章。一個'聯盟'可能是要走的路,我不太確定。這樣做的原因是我在'sites'表中有lat和lng字段,在'finds'表中有另一個字段,我想嘗試併合併到一個節點行中,然後將其加載到HTML頁面中。親切的問候 – IRHM

+0

@IHRM:說真的,正如Jim H所說,如果我們能夠建議一種合理的方法來查詢它們,我們確實需要查看您使用的表格的結構。 –

+0

嗨,我現在已經添加了表格結構到我原來的文章。親切的問候 – IRHM