mysql重复结果连接表

时间:2012-10-01 09:43:41

标签: mysql join

我正在为一个众所周知的购物车推出自己的功能。这是我的疑问:

    SELECT d.product, d.product_id, p.price FROM cscart_product_descriptions AS d, cscart_product_prices AS p WHERE d.product_id IN (
SELECT product_id FROM cscart_products WHERE product_code IN (
SELECT product_id FROM cscart_range WHERE range_name IN (
SELECT range_name FROM cscart_range WHERE product_id = '0140885'
)))

当我从第一个选择中删除product_prices相关选项时,我会按预期返回三个项目。当我使用包含的价格运行它时,我得到了4,500个结果。

我知道这是一个加入问题,我只是不确定如何纠正它。

干杯!

2 个答案:

答案 0 :(得分:1)

它与cscart_product_prices表进行交叉连接..

你可以尝试这样的事情:

 select a.*,p.price from (
      SELECT d.product, d.product_id 
      FROM cscart_product_descriptions AS d 
      WHERE d.product_id IN (
         SELECT product_id FROM cscart_products WHERE product_code IN (
         SELECT product_id FROM cscart_range WHERE range_name IN (
         SELECT range_name FROM cscart_range WHERE product_id = '0140885'
         )))a
join cscart_product_prices AS p
on p.product_id=a.product_id

答案 1 :(得分:0)

此外,尝试使用连接此查询 -

SELECT
  d.product, d.product_id, p.price
FROM
  cscart_product_descriptions d
JOIN cscart_products p
  ON p.product_id = d.product_id 
JOIN cscart_range r1
  ON r1.product_id = p.product_code
JOIN cscart_range r2
  ON r2.range_name = p.range_name
WHERE r2.product_id = '0140885'