[英]SQL joining if id value is not existing another table
I have to join Several table and tables data are below:我必须加入几个表和表数据如下:
Here is want to show all product with both regular and special price which product_id maybe not be exist in oc_special table if not exist it will show 0.000 price in result.这里要显示所有具有常规和特价的产品,其中 product_id 可能不存在于 oc_special 表中,如果不存在,结果将显示 0.000 价格。
Here is my what i am trying :这是我正在尝试的内容:
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
Here it only returns rows of items which are exists in oc_special table, but i want is to show all the product from oc_product table which product_id may not be exist in oc_special table.这里它只返回存在于 oc_special 表中的项目行,但我想要的是显示 oc_product 表中的所有产品,其中 product_id 可能不存在于 oc_special 表中。
If the results don't exist on ops table how do you expect to get ops.price
?如果结果在 ops 表中不存在,您如何期望得到ops.price
?
Anyway, if your current query is working like you say it is, you can quickly check the EXISTS
in the WHERE clause to filter the data.无论如何,如果您当前的查询像您说的那样工作,您可以快速检查 WHERE 子句中的EXISTS
以过滤数据。
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 NOT EXISTS(SELECT product_id FROM oc_product_special)
AND op.status = 1
GROUP BY op.product_id
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.