我正在尝试查询我的数据库,仅显示已收到至少60%订购手机的记录。
这是我的表的精简版:
dealID | Ordered | Received | precreditEL | cancelled | precredit
1 | 5 | 4 | | |
2 | 2 | 2 | | |
3 | 10 | 2 | | |
4 | 8 | 8 | Non-applicable | |
5 | 6 | 5 | Non-applicable | Cancelled | Ready
这是我当前的查询
select deals.dealID,customer_info.accName,account_info.accessid,account_info.access_password,customer_info.contactNumber,deals.est_bill,deals.precreditAssigned,deals.precreditEL,deals.precredit
from customer_info join deals
left join account_info on account_info.custID = customer_info.custID
left join phone_details on phone_details.dealID = deals.dealID
where customer_info.custID = deals.custID
and deals.precreditEL <> 'Non-applicable'
and deals.precredit <> 'Complete'
and deals.cancelled IS NULL
GROUP BY deals.dealID
having ((100 - (((sum(phone_details.ordered) - sum(phone_details.received)) / sum(phone_details.ordered)) * 100)) >= 60)
这完美无缺。只要precreditEL
不是非适用且未标记为已取消,查询将为我提供已收到订单的60%的记录。所以我的结果将是dealID 1 and 2
。
但现在我需要添加绕过这种情况的另一个条件。如果precredit
被标记为Ready
,则无论上述情况如何,结果都会显示记录。所以我的结果会显示dealID 1, 2 and 5
任何帮助将不胜感激。非常感谢你。
答案 0 :(得分:0)
没关系,我通过改变
得到了它having ((100 - (((sum(phone_details.ordered) - sum(phone_details.received)) / sum(phone_details.ordered)) * 100)) >= 60)
到
having (((100 - (((sum(phone_details.ordered) - sum(phone_details.received)) / sum(phone_details.ordered)) * 100)) >= 60) OR deals.precredit ='Ready')