2013-08-21 2 views
0

나는 두 개의 테이블이있다. 이 같은 .MySQL을 계산 수량

SELECT * FROM `equipments` AS a 
HAVING 
a.`e_quantity` > (SELECT COUNT(`equipments_booked`.`e_barcode`) 
FROM `equipments_booked` WHERE `equipments_booked`.`e_barcode`=a.`e_barcode`) 

답변

0

난 당신이 조인을 사용하려는 생각 집계 :

SELECT e.bar_code, count(eb.e_barcode) as quantity_booked 
FROM equipments e left join 
    equipments_booked eb 
    on e.e_barcode = eb.e_barcode 
GROUP BY e.bar_code; 

I

e_barcode, e_available_quantity(not total)

와 내가 작성한 쿼리

은 나에게 사용할 수있는 장비를 보여주고있다 논리가 당신의 having 절 뒤에 있는지 모르겠습니다.

+0

고든 Linoff : 고맙습니다 Lot.it 근무 ... :) 그것이 효과가 있습니다. –