简体   繁体   English

将另一个表连接到对我来说太复杂的SQL查询

[英]Joining one more table to a SQL query that's just too complex for me

I'm not all that well-versed in MySQL querying, and I usually only do very simple JOINs. 我并不精通MySQL查询,而且通常只做非常简单的JOIN。 I am working with an installation of osCommerce, and I want the category page to include products from all subcategories as well. 我正在安装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

Basically, I need to join the categories table to this query as well, pulling the row from the categories table where categories_id = p2c.categories_id. 基本上,我还需要将类别表也加入该查询,从类别表中拉出一行,其中category_id = p2c.categories_id。 Then, I can reference the parent_id column from the selected row from the categories table (I would replace the "###" above with something like "cat"). 然后,我可以从类别表的选定行中引用parent_id列(我将上面的“ ###”替换为“ cat”之类的东西)。

However, I'm getting confused with all the left joins as to where I should insert another JOIN clause. 但是,我对于应该在哪里插入另一个JOIN子句的所有左联接感到困惑。

Any help would be much appreciated. 任何帮助将非常感激。

Thanks! 谢谢!

Don't have the tables to test against, but should be something as simple as; 没有要测试的表,但应该像这样简单:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM