0

我对 MySQL 查询并不是那么精通,而且我通常只做非常简单的 JOIN。我正在安装 osCommerce,我希望类别页面也包含所有子类别的产品。

select
    p.products_image,
    pd.products_name,
    pd.products_description,
    p.products_id,
    p.manufacturers_id,
    p.products_price,
    p.products_tax_class_id,
    IF(s.status, s.specials_new_products_price, NULL) as specials_new_products_price,
    IF(s.status, s.specials_new_products_price, p.products_price) as final_price
from
    products_description pd,
    products p
left join
    manufacturers m on p.manufacturers_id = m.manufacturers_id
left join
    specials s on p.products_id = s.products_id,
    products_to_categories p2c
where
    p.products_status = '1' and
    p.products_id = p2c.products_id and
    pd.products_id = p2c.products_id and
    pd.language_id = '1' and
    (p2c.categories_id = '24' or ###.parent_id = '24')
order by pd.products_name asc

基本上,我还需要将类别表加入到此查询中,从类别表中的 category_id = p2c.categories_id 中提取行。然后,我可以从类别表中的选定行中引用 parent_id 列(我会将上面的“###”替换为“cat”之类的内容)。

但是,我对所有左连接感到困惑,因为我应该在哪里插入另一个 JOIN 子句。

任何帮助将非常感激。

谢谢!

4

1 回答 1

1

没有要测试的表格,但应该很简单;

SELECT
    p.products_image,
    ...
FROM products_description pd
JOIN products p
  ON pd.products_id = p.products_id
LEFT JOIN manufacturers m 
  ON p.manufacturers_id = m.manufacturers_id
LEFT JOIN specials s 
  ON p.products_id = s.products_id
JOIN products_to_categories p2c
  ON p2c.products_id = p.products_id
JOIN categories c
  ON c.categories_id = p2c.categories_id
WHERE
    p.products_status = '1' and
    pd.language_id = '1' and
    (p2c.categories_id = '24' or c.parent_id = '24')
ORDER BY by pd.products_name ASC
于 2013-06-24T21:12:18.777 回答