两个连接语句给出了Unknown列

时间:2017-07-03 14:55:06

标签: mysql sql inner-join

我有这个SQL查询:

SELECT `main_table`.* FROM `ves_brand` AS `main_table`
INNER JOIN 
    (SELECT DISTINCT value from catalog_product_entity_int 
    where row_id in 
        (select row_id from catalog_product_entity_int 
        WHERE (attribute_id, value) IN ( (99, 4) ))) AS `t` 
ON main_table.brand_id = t.value 
INNER JOIN `catalog_product_entity` AS `cpeiz` 
ON cpeiz.row_id = catalog_product_entity_int.row_id 
WHERE (cpeiz.attribute_set_id != 19)

这给出了错误: #1054 - Unknown column 'catalog_product_entity_int.row_id' in 'on clause'

即使我为列catalog_product_entity_int.row_id设置了别名,该列也总是"未知"。

1 个答案:

答案 0 :(得分:2)

在您的子查询中定义了

catalog_product_entity_int,您无法直接从子查询外部引用它。 (想象它就像一个局部变量,你必须以某种方式将它传回主查询)

非常依赖于您的数据集,但这样的事情将起作用

SELECT `main_table`.* FROM `ves_brand` AS `main_table`
INNER JOIN 
    (SELECT DISTINCT value, row_id from catalog_product_entity_int 
    where row_id in 
        (select row_id from catalog_product_entity_int 
        WHERE (attribute_id, value) IN ( (99, 4) ))) AS `t` 
ON main_table.brand_id = t.value 
INNER JOIN `catalog_product_entity` AS `cpeiz` 
ON cpeiz.row_id = t.row_id 
WHERE (cpeiz.attribute_set_id != 19)