我想选择最小可用数量等于零的所有产品名称

时间:2015-10-30 06:19:54

标签: php mysql

我有两个数据库表nrk_productnrk_stock

我想从最小可用数量等于零的nrk_product表中选择所有这些产品名称。

我正在编写以下查询,它向我展示了可用数量大于零的那些记录,但我想要反过来。

SELECT * FROM `nrk_product` AS `np`     
    LEFT JOIN `nrk_stock`  AS `ns`  
    ON `np`.`id` = `ns`.`product_id`        
    GROUP BY `np`.`id`
    HAVING (SUM(`ns`.`credit_quantity`) - SUM(`ns`.`debit_quantity`)) > 0 

3 个答案:

答案 0 :(得分:1)

如果你只是想要反过来而不是只用大于(>)替换小于等于(< =)

SELECT * FROM `nrk_product` AS `np`     
    LEFT JOIN `nrk_stock`  AS `ns`  
    ON `np`.`id` = `ns`.`product_id`        
    GROUP BY `np`.`id`
    HAVING (SUM(`ns`.`credit_quantity`) - SUM(`ns`.`debit_quantity`)) <= 0

答案 1 :(得分:0)

你可以试试这个: -

=0 要么 < 0

SELECT nrk_product FROM nrk_product AS np
LEFT JOIN nrk_stock AS ns
ON np.id = ns.product_id
GROUP BY np.id HAVING (SUM(ns.credit_quantity) - SUM(ns.debit_quantity)) = 0

答案 2 :(得分:0)

SELECT * FROM `nrk_product` AS `np`     
    LEFT JOIN `nrk_stock`  AS `ns`  
    ON `np`.`id` = `ns`.`product_id`        
    GROUP BY `np`.`id`
    HAVING (SUM(`ns`.`credit_quantity`) - SUM(`ns`.`debit_quantity`)) < 1