【发布时间】:2020-01-08 11:52:52
【问题描述】:
我要加入几个表,表数据如下:
这里要显示所有具有常规和特价的产品,如果 oc_special 表中不存在 product_id 可能不存在,结果将显示 0.000 价格。
这是我正在尝试的:
SELECT op.product_id, op.model, op.image, op.price, ops.price as discount_price, opc.category_id, opd.name as product_name, opd.description, ocd.name as cat_name FROM oc_product op INNER JOIN oc_product_to_category opc ON opc.product_id = op.product_id INNER JOIN oc_product_description opd ON opd.product_id = op.product_id INNER JOIN oc_category_description ocd ON ocd.category_id = opc.category_id Inner JOIN oc_product_special ops ON op.product_id = ops.product_id where op.status = 1 GROUP BY op.product_id
这里它只返回存在于 oc_special 表中的项目行,但我想要显示 oc_product 表中的所有产品,其中 product_id 可能不存在于 oc_special 表中。
【问题讨论】:
-
(1) 我不清楚你的问题是什么。尽管表格布局可能会有所帮助,但样本数据和所需结果通常更有帮助。 (2) 你用的是什么数据库?
-
听起来您需要 OUTER JOIN 而不是 INNER JOIN?
-
al_sweets 的查询是什么
-
假设 oc_product 表上有 8 个产品,oc_product_special 表上有 4 个产品。现在我想显示 oc_product 表中的所有这 8 种产品,其中 4 种不在 oc_product_special 表中。我正在使用mysql数据库@GordonLinoff