我用联接编写了一个选择查询,该查询给出了100条记录,但是我需要获取从DB提取相同查询的记录数
我尝试了以下提到的问题,但计数错误。
SELECT COUNT(vm.vendor_id)
FROM vendor_master vm LEFT JOIN purchase_order po ON po.vendor_id=vm.vendor_id AND vm.`is_active`=TRUE AND vm.`organization_id`=1
SELECT vm.vendor_id vendor_id,COUNT(po.Purchase_id) TOTAL_PURCHASES, vm.Vendor_Name,
MAX((SELECT vc.Contact_Name FROM vendor_contacts vc WHERE po.vendor_cont_id=vc.Vend_Cont_id)) contact_name,
SUM((SELECT SUM(pd.pur_amt) FROM purchase_details pd WHERE po.Purchase_id=pd.Purchase_id)) Total_amount,
(SELECT SUM(pm.Amount) FROM payment_master pm WHERE pm.Vendor_id = vm.vendor_id) Paid,
(SUM((SELECT SUM(pd.pur_amt) FROM purchase_details pd WHERE po.Purchase_id=pd.Purchase_id)) -
(SELECT SUM(pm.Amount) FROM payment_master pm WHERE pm.Vendor_id = vm.vendor_id)) Balance
FROM vendor_master vm LEFT JOIN purchase_order po ON po.vendor_id=vm.vendor_id AND vm.`is_active`=TRUE AND vm.`organization_id`=1
GROUP BY vm.vendor_id
上面是我需要从中获取计数的实际查询
答案 0 :(得分:1)
SELECT COUNT(*) FROM
(
SELECT vm.vendor_id vendor_id,COUNT(po.Purchase_id) TOTAL_PURCHASES, vm.Vendor_Name,
MAX((SELECT vc.Contact_Name FROM vendor_contacts vc WHERE po.vendor_cont_id=vc.Vend_Cont_id)) contact_name,
SUM((SELECT SUM(pd.pur_amt) FROM purchase_details pd WHERE po.Purchase_id=pd.Purchase_id)) Total_amount,
(SELECT SUM(pm.Amount) FROM payment_master pm WHERE pm.Vendor_id = vm.vendor_id) Paid,
(SUM((SELECT SUM(pd.pur_amt) FROM purchase_details pd WHERE po.Purchase_id=pd.Purchase_id)) -
(SELECT SUM(pm.Amount) FROM payment_master pm WHERE pm.Vendor_id = vm.vendor_id)) Balance
FROM vendor_master vm LEFT JOIN purchase_order po ON po.vendor_id=vm.vendor_id AND vm.`is_active`=TRUE AND vm.`organization_id`=1
GROUP BY vm.vendor_id
) a;