【发布时间】:2013-06-21 12:23:33
【问题描述】:
我不是很精通 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 子句。
任何帮助将不胜感激。
谢谢!
【问题讨论】:
-
。 .如果您对查询进行了格式化,您可能会发现它们更容易修改和维护。
-
我愿意。 osCommerce 喜欢将它们排在非常可笑的长线上。
-
那里。我重新格式化了我的问题中的示例查询。
-
只需在现有的 JOIN 之后拍下你的下一个 JOIN。另外,帮自己一个忙,取消不推荐使用的连接语法。
-
您混合了两种不同的连接语法样式。任选其一,不要混用。一旦你有了一致的风格,你的问题基本上就解决了。
标签: php sql left-join oscommerce