2011-10-30 6 views
0

データベースのレイアウトは次のとおりです。私はDBにうまく接続することができます。 私はデータベースから2つのものを引き出していましたが、3番目を追加すると引き出すことができません。私はいくつかの助け私はできれば..私は、他の2つのテーブルの名前と、他の情報を引っ張るしようとしていますmysqlを使用して3つのテーブルからデータを取り出す方法の設定

database - mmoore_drupal 

table 1 

table name = content_type_uprofile 

data 
vid= 19723 
nid =19674 
field_name_value = matthew moore 


table 2 

table name = location_instance 

data 

lid = 1521 
vid = 19723 
nid = 19674 

table 3 

table name = location 

data 

lid = 1521 
street = 
city = 
country = 
latitude = 
longitude = 

を必要としています。しかし、私は主に私は場所から名前と他の情報を持っている必要があります。私は接続を関連付けるために別のテーブルを持っていなければならないと思った。どんな助けもありがとうございます。

$query = "SELECT content_type_uprofile.field_name_value,location.street,location.city 
    FROM location_instance,location,content_type_uprofile 
     WHERE location_instance.lid = location.lid and        location_instance.nid=content_type_uprofile.nid" 

$result = mysql_query($query) or die(mysql_error()); 


// Print out the contents of each row into a table 
while($row = mysql_fetch_array($result)){ 
    echo $row['nid']."-".$row['street']. " - ". $row['city']; 
    echo "<br />"; 
} 
?> 

答えて

1

使用このSQL(明示的な結合構文):あなたが投稿

SELECT 
    content_type_uprofile.nid, 
    location.street, 
    location.city 
FROM 
    content_type_uprofile 
INNER JOIN location_instance 
    ON (content_type_uprofile.nid = location_instance.nid) 
INNER JOIN location 
    ON (location_instance.lid = location.lid) 

SQLはimplicit join SQL syntaxを使用しています。

私はいくつかの理由のためだと思う、私はあなたのSQLの行を考える:

WHERE location_instance.lid = location.lid and location_instance.nid=content_type_uprofile.nid 

は、あなたの結果セットからすべての行を除外しています。私は暗黙の構文を避けるため、わからない。

また、結果セットでPHPコードが探しているnidフィールドもありませんでした。

データが正しい場合(つまり、参加しているフィールドに適切な値が設定されている場合)、投稿したSQLが機能します。

+0

それは結果である40行目のエラーです。 –

+0

クエリツールにSQLを貼り付ける必要があります:http://dev.mysql.com/downloads/workbench/、http://www.quest.com/toad-for-mysql/、http:// www .phpmyadmin.net/home_page/index.phpは人気のある無料のものです。これにより、エラーを見つけるのに役立ちます。私はこれをしなかったが、私のSQLを校正すると、 'location.city'の後に余分なカンマがあることに気づいた。私はそれを訂正した。もう一度お試しください。 – JohnB

+0

新しいツールを使用していただきありがとうございます。 –

-1

joinさんはこれまでに何かしましたか?

select *.locaition, 
    *.content_type_uprofile 
from location_instance li 
inner join location l on l.lid = li.lid 
inner join content_type_uprofile ctu on ctu.vid = li.vid 
+0

'場所。*、content_type_uprofile。*' - あなたはそれらを元に戻しました。 – JohnB

関連する問題