mysql错误:将内部联接的结果用于新查询

时间:2013-10-30 14:30:16

标签: mysql inner-join

我想要组合2个sql字符串:

  • 第一个表格包含产品的所有数据
  • 第二个表格(inner join)包含具有组价格的所有产品数据。

但是我得到了不同的结果

测试搜索'sku'

SELECT sku
FROM  `c3eiwitlive`.`catalog_product_entity` 
WHERE (
 `entity_id` LIKE  '%17086%'
)

group_price

上的直接sql

结果269条记录

SELECT SKU, value
FROM  `c3eiwitlive`.`catalog_product_entity_group_price` AS T1
INNER JOIN
`c3eiwitlive`.`catalog_product_entity` AS T2 
ON T1.`entity_id` = T2.`entity_id`;

组合sql返回较少的记录

结果234条记录

SELECT name
FROM  `c3eiwitlive`.`catalog_product_flat_1` AS T1
WHERE T1.`sku` 
IN (
    SELECT sku
    FROM  `c3eiwitlive`.`catalog_product_entity_group_price` AS T1
    INNER JOIN  `c3eiwitlive`.`catalog_product_entity` AS T2 ON T1.`entity_id` = T2.`entity_id`
)

1 个答案:

答案 0 :(得分:0)

在我看来,'catalog_product_flat_1'表中缺少一些sku条目。 为什么不反转查询以识别丢失的行?

SELECT sku
FROM  `c3eiwitlive`.`catalog_product_entity_group_price` AS T1
INNER JOIN  `c3eiwitlive`.`catalog_product_entity` AS T2 ON T1.`entity_id` = T2.`entity_id`
WHERE sku 
NOT IN (
      SELECT sku from `c3eiwitlive`.`catalog_product_flat_1`
)