The following query only work when I have a reference in the table wm_purchased_products.purchased_article_id
but when this is empty mysql_num_rows return 0
The query is:
SELECT (SUM(wm_products_quantities.new_quantity) - SUM(wm_purchased_products.purchased_article_total) ) AS stock_restante,
wm_products_wall.nombre,
wm_products_wall.detalles,
wm_products_wall.price,
wm_products_wall.image_full,
wm_products_wall.fecha,
wm_products_wall.article_hashid
FROM wm_products_wall,
wm_prod开发者_如何学编程ucts_quantities,
wm_purchased_products
WHERE wm_products_wall.categoria = '$new_rquery_xp'
AND wm_products_wall.article_hashid = wm_products_quantities.hashid_ref
AND wm_products_wall.article_hashid = wm_purchased_products.purchased_article_id
GROUP BY wm_products_wall.article_hashid
ORDER BY stock_restante ASC
How to build this query to work when I have no record in the table wm_purchased_products.purchased_article_id
Your code FROM wm_products_wall, wm_products_quantities, wm_purchased_products
means all three table are INNER JOIN with each other.(that is, each and every row in the first table is joined to each and every row in the second table and so on.)
You can let wm_products_wall LEFT JOIN with wm_purchased_products, so do wm_purchased_products.
SELECT
(SUM(wm_products_quantities.new_quantity) - SUM(wm_purchased_products.purchased_article_total) ) AS stock_restante,
wm_products_wall.nombre, wm_products_wall.detalles, wm_products_wall.price,
wm_products_wall.image_full, wm_products_wall.fecha,
wm_products_wall.article_hashid
FROM wm_products_wall
LEFT OUTER JOIN wm_products_quantities
ON wm_products_wall.article_hashid = wm_products_quantities.hashid_ref
LEFT OUTER JOIN wm_purchased_products
ON wm_products_wall.article_hashid = wm_purchased_products.purchased_article_id
WHERE wm_products_wall.categoria = '$new_rquery_xp'
GROUP BY wm_products_wall.article_hashid
ORDER BY stock_restante ASC
精彩评论