简体   繁体   English

4 mysql表联接?

[英]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. 我有4张桌子,不知何故需要加入它们。 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}. 我需要从一个表中获取red_value,blue_value和green值,其中另一个表的ID = {some number},而图层= {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? 是因为您要联接4个表,并且您认为这是要联接的许多表吗? 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. 我认为4个表并不是要连接的大量表,特别是因为您似乎正在使用主键进行连接。 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. 在规范化方面,出现在表set_colorbaseimage中的列layer在这些表baseimage似乎是多余的。 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. 除非我误解了您要传达的内容,否则它是依赖于baseimage_id的属性,但不是标识属性。 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. 如果要查询未出现layer的表,并且需要找出layer的值,则只需联接到找到该表的表即可。

EDIT: 编辑:

WHERE set_colors.{$layer} = baseimage.layer

Is this really what you mean? 这真的是你的意思吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM