4 mysql table joins?
I have a complicated query (I think) and whether there is a better way of going about this, because clearly this is not working.
I have 4 tables and somehow need to join them. I need to get the red_value, blue_value, and green values from one table where the id of another table = {some number} and the layer = {some number}.
Here are the tables:
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)**
Here is the code:
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;
}
So what I HOPE to do is to call the 开发者_如何转开发class and
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}
Thank you in advance.
You say "clearly this is not working". It is not clear what you mean by this. How did you arrive at this conclusion? Is it because you are joining 4 tables, and you regard this as a great many tables to join? I do not think 4 tables is a staggeringly large number of tables to join, particularly since you seem to be joining using primary keys. A better question would be whether the database design is normalised. Assuming that it is, I can't see any real problem with the posted query.
On the subject of normalisation, the column layer
that appears in the tables set_color
and baseimage
appears to be redundant in one of those tables. Unless I've misunderstood what you're trying to communicate, it's an attribute dependent on the baseimage_id
but it isn't an identifying attribute. So, it should be removed from one of those tables. If you're querying the table in which layer
doesn't appear, and need to find out the value of layer
, you need only join to the table where it is found.
EDIT:
WHERE set_colors.{$layer} = baseimage.layer
Is this really what you mean?
精彩评论