mysql union语句

时间:2011-11-25 18:05:43

标签: mysql unions derived-table

任何人都可以看到这个明显的问题,因为我不能。

以下不提供任何行。即使我离开WHERE attribute_id LIKE @aid AND value LIKE @oid;并使用SELECT * FROM,也不会返回任何内容

SELECT @aid:=a.attribute_id,@oid:=v.option_id FROM eav_attribute_option  AS o
JOIN eav_attribute_option_value AS v ON o.option_id = v.option_id
JOIN eav_attribute AS a ON o.attribute_id = a.attribute_id
WHERE v.value LIKE 'aoc'
AND a.attribute_code = 'brand';

SELECT DISTINCT entity_id
FROM ( 
 SELECT entity_id,attribute_id,value FROM `catalog_product_entity_datetime`
 UNION DISTINCT
 SELECT entity_id,attribute_id,value FROM `catalog_product_entity_decimal`
 UNION DISTINCT
 SELECT entity_id,attribute_id,value FROM `catalog_product_entity_gallery`
 UNION DISTINCT
 SELECT entity_id,attribute_id,value FROM `catalog_product_entity_int`
 UNION DISTINCT
 SELECT entity_id,attribute_id,value FROM `catalog_product_entity_media_gallery`
 UNION DISTINCT
 SELECT entity_id,attribute_id,value FROM `catalog_product_entity_text`
 UNION DISTINCT
 SELECT entity_id,attribute_id,value FROM `catalog_product_entity_varchar`
) AS tbl
WHERE attribute_id LIKE @aid AND value LIKE @oid;

如果我将派生表放在圆括号之外,我会返回5行。以下作品

SELECT @aid:=a.attribute_id,@oid:=v.option_id FROM eav_attribute_option  AS o
JOIN eav_attribute_option_value AS v ON o.option_id = v.option_id
JOIN eav_attribute AS a ON o.attribute_id = a.attribute_id
WHERE v.value LIKE 'aoc'
AND a.attribute_code = 'brand';

SELECT entity_id FROM `catalog_product_entity_datetime` WHERE attribute_id = @aid AND value = @oid
UNION DISTINCT
SELECT entity_id FROM `catalog_product_entity_decimal` WHERE attribute_id = @aid AND value = @oid
UNION DISTINCT
SELECT entity_id FROM `catalog_product_entity_gallery` WHERE attribute_id = @aid AND value = @oid
UNION DISTINCT
SELECT entity_id FROM `catalog_product_entity_int` WHERE attribute_id = @aid AND value = @oid
UNION DISTINCT
SELECT entity_id FROM `catalog_product_entity_media_gallery` WHERE attribute_id = @aid AND value = @oid
UNION DISTINCT
SELECT entity_id FROM `catalog_product_entity_text` WHERE attribute_id = @aid AND value = @oid
UNION DISTINCT
SELECT entity_id FROM `catalog_product_entity_varchar` WHERE attribute_id = @aid AND value = @oid

但它似乎不是一个很好的方法。

0 个答案:

没有答案