MySQL在查询中从第三个表获取值时遇到麻烦

时间:2014-05-16 13:42:19

标签: mysql sql

我需要帮助从第三个表中检索值

product_attribute
| id_product_attribute | id_product | reference | ean13 |

product_attribute_combination
| id_attribute | id_product_attribute |

attribute_lang
| id_attribute | name |

我在下面进行了查询以获取产品列表,但我也希望从attribute_lang获取name并且我不知道如何。

有人可以帮我吗?

这就是我现在所拥有的

SELECT T1.id_product, T1.reference, T2.name, T1.price, IF(LENGTH(TRIM(T1.ean13)) = 0, T1.id_product, T1.ean13) AS ean13
        FROM /*PREFIX*/product_attribute T1
        INNER JOIN /*PREFIX*/product_lang T2 ON (T1.id_product = T2.id_product AND T2.id_lang = /*CURRENT_LANGUAGE_ID*/) 
        WHERE T1.id_product /*PRODUCTS_ID_LIST*/

1 个答案:

答案 0 :(得分:0)

您需要再添加2个连接:

LEFT JOIN /*PREFIX*/product_attribute_combination T3 ON T3.id_product_attribute=T1.id_product_attribute
LEFT JOIN /*PREFIX*/attribute_lang T4 ON T4.id_attribute=T3.id_attribute

并将T4.name添加为选定列

结果将是这样的:

SELECT T1.id_product, T1.reference, T2.name, T1.price, IF(LENGTH(TRIM(T1.ean13)) = 0, T1.id_product, T1.ean13) AS ean13, T4.name
FROM /*PREFIX*/product_attribute T1
INNER JOIN /*PREFIX*/product_lang T2 ON (T1.id_product = T2.id_product AND T2.id_lang = /*CURRENT_LANGUAGE_ID*/)
LEFT JOIN /*PREFIX*/product_attribute_combination T3 ON T3.id_product_attribute=T1.id_product_attribute
LEFT JOIN /*PREFIX*/attribute_lang T4 ON T4.id_attribute=T3.id_attribute
WHERE T1.id_product /*PRODUCTS_ID_LIST*/