Php Mysql返回值之和小于另一个表中的值
我需要检索所有产品,其中Php Mysql返回值之和小于另一个表中的值,php,mysql,Php,Mysql,我需要检索所有产品,其中Alert为真,MininumAlert值为每个商店和StoreDetail中产品的qnty之和。Search=1。例如: 表产品: ------------------------------------- | ID | Name | MinimumAlert | Alert | ------------------------------------- | 1 | Prod1 | 100 | 1 | -------------------
Alert
为真,MininumAlert
值为每个商店和StoreDetail中产品的qnty之和。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 |
表2详细信息:
------------------------------
| 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个查询:1个在get\u all\u products
func内,另1个在get\u stores\u by\u prod\u ID
func内):
函数在最小值()下获取产品{
$result=array();
$all_products=get_all_products();//从产品中选择*
foreach($product作为$product的所有产品){
如果($product['Alert'])){
$total=0;
$all_stores_product=get_stores_by_prod_ID($product['ID']);//从prod_ID=$product['ID']的门店中选择*
foreach($all\u stores\u产品为$store){
$info_store=get_info_store($store['store_ID']);//从StoreDetail中选择*,其中ID=?
如果($info_store['Search'])){
$total=$total+$store['Qty'];
}
}
如果($total<$product['MinimumAlert'])){
$result[]=$product['ID'];
}
}
}
返回$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
ID 1原因警报为真,搜索为真,总和(5+90)小于100
ID 3原因警报为真,搜索为真,总和(450)小于500
ID 5原因警报为真,搜索为真,总和(98+2+3)小于110
如果可能的话,我希望只使用一个查询来使用表之间的适当关系连接表。然后,我们可以在产品ID、名称和MinimumAlert上按
分组(仅与\u full\u GROUP\u BY mode兼容)
我们可以使用<代码> SUM()/代码>函数来计算所有商店的总<代码> QTy <代码>,并使用<代码>具有子句来考虑那些代码<>最小警告> <代码>大于总存储量的产品。
选择p.ID、p.Name、p.MinimumAlert、,
总计(s.Qty)作为总门店数量
来自产品p
在s.Prod_ID=p.ID上加入存储
在sd.ID=s.Store\u ID上加入storedetail sd
其中p.alert=1和
sd.search=1
按p.ID、p.Name、p.MinimumAlert分组
p.MinimumAlert>总门店数量
我想你可以通过使用having
和groupby
进行一次查询来实现这一点。嗨,谢谢你的回复。对不起,我忘了另一张桌子。我现在添加了:StoreDetail。我还需要通过Search=true进行筛选。@GiuseppeLodiRizzini您只需再添加一个连接条件即可
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