2011-07-21 46 views
0

私のmysqlデータベースで5つのテーブルを照会しようとしていますが、構造は次のとおりです。mysqlを使用して複数のテーブルをクエリする

item 
itemid | item | description | brand | date | time | path | 

actor 
actorid | name | actorthumb | bio | 

brand 
brandid | brandname | description | image | 

movie 
movieid | title | genre | year | moviethumb | synopsis| 

request 
requestid | userid | itemid | brandid | movieid | actorid | content | requestdate | 


現在、私はテーブルの2を結合し、例えば、それを身に着けているアイテム、私は必要な情報を表示することができます。

を使用して、

$query = "SELECT * FROM actor, request WHERE actor.actorid = request.actorid and itemid = ".$itemid; 

と何の映画での

$query = "SELECT distinct * FROM movie, request WHERE movie.movieid = request.movieid and itemid = ".$itemid; 


しかし、私は5つのテーブルすべてからのデータを表示する1つのクエリを書く必要があり、私は必要なものを表示することができますこれらから。

私はJOINコマンドを使用する必要があると思いますが、これをどのように使用するかわかりません。

お知らせください。

+3

[Join on MySQL Documentation](http://dev.mysql.com/doc/refman/5.0/ja/join.html)を読んでいますか? –

+1

だから、あなたはちょっと、 "クリスパー・プラマーを大佐として出演し、蝋燭の研究(ティファニー製)で主演した"手がかり、映画を探していますか? –

+0

はい、私は理想的には、映画、俳優、それを着ている服、アイテムなどのブランドで着用しているアイテムを表示したい –

答えて

2

これは、IDを使用してさまざまなテーブルを参照する方法を示す非常に単純なクエリ構造です。それは非常にあなたが設定され、よりフィルタリングされた結果を返すしたい場合は、このような何かを追加することができますあなたは

SELECT 
i.*, /* This will display all the fields in the item table */ 
a.*, /* This will display all the fields in the actor table */ 
b.*, /* This will display all the fields in the brand table */ 
m.*, /* This will display all the fields in the movie table */ 
r.* /* This will display all the fields in the request table */ 
FROM item AS i, actor AS a, brand AS b, movie AS m, request AS r 
/* This joins the request table itemid with the item table itemid */ 
WHERE r.itemid = i.itemid 
/* This joins the request table actorid with the actor table actorid */ 
AND r.actorid = a.actorid 
/* This joins the request table brandid with the brand table brandid */ 
AND r.brandid = b.brandid 
/* This joins the request table movieid with the movie table movieid */ 
AND r.movieid = m.movieid 

を望む結果に応じて、大幅に改善することができます。

/* Or whatever the id is */ 
AND r.requestid = 123 

例:

SELECT 
i.item, i.description, i.brand, /* item table */ 
a.name, /* actor table */ 
b.brandname, b.description, b.image, /* brand table */ 
m.title /* movie table */ 
FROM item AS i, actor AS a, brand AS b, movie AS m, request AS r 
/* This joins the request table itemid with the item table itemid */ 
WHERE r.itemid = i.itemid 
/* This joins the request table actorid with the actor table actorid */ 
AND r.actorid = a.actorid 
/* This joins the request table brandid with the brand table brandid */ 
AND r.brandid = b.brandid 
/* This joins the request table movieid with the movie table movieid */ 
AND r.movieid = m.movieid 
AND a.name = 'Rosie Huntington-Whiteley'; 
関連する問題