2011-08-02 95 views
1

我有一個複雜的查詢(我認爲)以及是否有更好的方法來解決這個問題,因爲顯然這不起作用。4 mysql表連接?

我有4個表,不知何故需要加入他們。我需要從一個表中獲取red_value,blue_value和green值,其中另一個表的ID = {某個數字}和圖層= {某個數字}。

下面是表:

product_color: 
    **color_id (primary)** 
    red_value 
    green_value 
    blue_value 

set_color: 
    **setcolors_id(primary)** 
    **school_art_id (school_art -primary key)** 
    **baseimage_id (baseimage - primary key)** 
    **color_id (product_color - primary key)** 
    layer (same number value as the layer in the "baseimage"table) 

baseimage: 
    **id (primary key)** 
    layer (same value as layer in "set_color") 

school_art: 
**id (primary key)** 

下面是代碼:

public function select_colors($value, $layer) { 
global $db; 
$result_array = mysql_query(" 
    SELECT * 
    FROM set_colors 
    INNER JOIN school_art ON set_colors.{$value} = school_art.id 
    INNER JOIN base_product_color ON set_colors.color_id = base_product_color.color_id; 
    INNER JOIN mbaseimage ON set_colors.baseimage_id = baseimage.id  
    WHERE set_colors.{$layer} = baseimage.layer 
    " 
); 
return $result_array; 

}

所以我希望做的是調用類和

get the red_value, green_value and blue_value from the "product_color" table 
WHERE the "color_id" = the "color_id" of the "set_colors" 
and "school_art_id" = the {$value} 
and "layer" = {$layer} 

預先感謝您。

+0

那麼究竟是什麼問題呢? 它不會產生正確的結果嗎?它太慢了嗎? – andsens

+0

沒有得到任何結果。沒有錯誤。但沒有結果 – GGcupie

+0

你傳遞給函數的'$ value'和'$ layer'的值是什麼? – Hammerite

回答

0

你說「顯然這不起作用」。目前尚不清楚你的意思。你是如何得出這個結論的?是因爲你加入了4張桌子,你認爲這是很多加入的桌子嗎?我不認爲4表是一個驚人的大量表加入,特別是因爲你似乎正在使用主鍵加入。更好的問題是數據庫設計是否標準化。假設它是,我看不到發佈的查詢有任何真正的問題。

關於標準化問題,表set_colorbaseimage中出現的列layer在其中一個表中似乎是冗餘的。除非我誤解了你想要溝通的內容,這是一個取決於baseimage_id的屬性,但它不是一個識別屬性。所以,它應該從其中一個表中刪除。如果您要查詢沒有出現layer的表格,並且需要查找layer的值,則只需加入找到該表格的表格即可。

編輯:

WHERE set_colors.{$layer} = baseimage.layer 

這真的是你是什麼意思?