2013-03-15 2 views
0

주문한 전화 중 최소한 60 %가 수신 된 레코드 만 표시하도록 데이터베이스를 쿼리하려고합니다.두 조건을 가진 mysql 쿼리

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입니다.

하지만 이제이 조건을 우회하는 다른 조건을 추가해야합니다. precreditReady으로 표시된 경우 결과는 위의 조건과 관계없이 레코드를 표시합니다. 그래서 내 결과가 표시됩니다 dealID 1, 2 and 5

도움이 되길 바랍니다. 고맙습니다.

답변

0

신경 끄시 고, 내가

having (((100 - (((sum(phone_details.ordered) - sum(phone_details.received))/sum(phone_details.ordered)) * 100)) >= 60) OR deals.precredit ='Ready') 

having ((100 - (((sum(phone_details.ordered) - sum(phone_details.received))/sum(phone_details.ordered)) * 100)) >= 60) 

을 변경하여 그것을 가지고

관련 문제