MySQL返回值的总和小于另一个表的值

时间:2018-11-27 13:35:58

标签: php mysql

在一种情况下,我需要在Alert为TRUE的情况下检索所有产品,而MininumAlert的值是每个商店中Storen.StoreDetail.Search = 1的产品数量之和的<。例如:

餐桌产品:

-------------------------------------
| ID | Name  | MinimumAlert | Alert |
-------------------------------------
| 1  | Prod1 |   100        |   1   |
-------------------------------------
| 2  | Prod2 |   150        |   1   |
-------------------------------------
| 3  | Prod3 |   500        |   1   |
-------------------------------------
| 4  | Prod4 |              |   0   |
-------------------------------------
| 5  | Prod5 |   110        |   1   |

表StoreDetail:

------------------------------
| ID | Name  | Search       | 
-----------------------------
| 100  | Store1|   1          |   
------------------------------
| 101  | Store2|   1          |  
------------------------------
| 102  | Store3|   1          |  
-----------------------------
| 103  | Store4|   1          |  
------------------------------
| 104  | Store5|   1          | 

表存储:

-------------------------------------
| ID | Store_ID| Qty          | Prod_ID|
-------------------------------------
| 1  | 100     |   5          |   1   |
-------------------------------------
| 2  | 101     |   90         |   1   |
-------------------------------------
| 4  | 100     |   400        |   2   |
-------------------------------------
| 5  | 101     |   30         |   2   |
-------------------------------------
| 6  | 100     |   450         |   3   |
 -------------------------------------
| 7  | 100     |   99         |   4   |
 -------------------------------------
| 8  | 100     |   98         |   5   |
 -------------------------------------
| 9  | 101     |   2          |   5   |
-------------------------------------
| 10  | 102     |   3          |   5   |

我不知道如何从“一个查询”中的(产品表)中检索ID。 我实际上在PHP中使用了这样的函数(使用2个查询:get_all_products函数内部有1个,get_stores_by_prod_ID函数内部有1个):

function get_products_under_minimum() {

   $result = array();

   $all_products = get_all_products(); //SELECT * FROM Product

   foreach ( $all_products as $product ) {
     if ( $product['Alert'] ) {
       $total = 0;
       $all_stores_product = get_stores_by_prod_ID( $product['ID'] ); // SELECT * FROM Store WHERE Prod_ID = $product['ID']
       foreach ( $all_stores_product as $store) {
           $info_store = get_info_store($store['Store_ID']); // SELECT * FROM StoreDetail WHERE ID = ?
           if ( $info_store['Search'] ) {
            $total = $total + $store['Qty'];
           }
       }

       if ( $total < $product['MinimumAlert'] ) {
          $result[] = $product['ID'];
       }

     }
   }

   return $result;

}

根据我的规则,我需要检索:

ID 1 cause Alert is true, Search is true and SUM(5+90) is < 100
ID 3 cause Alert is true, Search is true and SUM(450) is < 500
ID 5 cause Alert is true, Search is true and SUM(98+2+3) is < 110

如果可能的话,我想只使用一个QUERY

1 个答案:

答案 0 :(得分:3)

使用它们之间的适当关系在表之间

JOIN。然后,我们可以GROUP BY上的产品ID,名称和MinimumAlert(与only_full_group_by模式兼容)。

然后我们可以使用SUM()函数来计算所有商店中的Qty总数,并使用HAVING子句考虑其中MinimumAlert大于总数的那些产品仓库数量。

SELECT p.ID, p.Name, p.MinimumAlert, 
       SUM(s.Qty) AS total_store_qty 
FROM product p
JOIN store s ON s.Prod_ID = p.ID 
JOIN storedetail sd ON sd.ID = s.Store_ID 
WHERE p.alert = 1 AND 
      sd.search = 1 
GROUP BY p.ID, p.Name, p.MinimumAlert
HAVING p.MinimumAlert > total_store_qty