我有这个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
设置了别名,该列也总是"未知"。
答案 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)