SELECT DISTINCT A.*,IFNULL(orderTab.OrderNum,0) AS OrderNum
FROM RB_Reserve_ServicePersonal AS A
INNER JOIN
(
SELECT * FROM rb_reserve_servicepersonalproduct WHERE ServiceType=3 AND `Status`=0
UNION
SELECT * FROM rb_reserve_servicepersonalproduct WHERE ServiceType=2 AND `Status`=0 AND ProductId={0}
UNION
SELECT * FROM rb_reserve_servicepersonalproduct WHERE ServiceType=1 AND `Status`=0 AND ProductId IN({1})
) AS B ON A.ID=B.ServiceId
SELECT DISTINCT A.*,IFNULL(orderTab.OrderNum,0) AS OrderNum
FROM(
SELECT A.*
FROM RB_Reserve_ServicePersonal AS A
WHERE A.Status=0 AND A.ServiceType=3
UNION ALL
SELECT A.*
FROM RB_Reserve_ServicePersonal AS A INNER JOIN rb_reserve_servicepersonalproduct AS C ON A.ServiceType=C.ServiceType AND C.`Status`=0 AND C.ProductId={0}
WHERE A.Status=0 AND A.ServiceType=2
UNION ALL
SELECT A.*
FROM RB_Reserve_ServicePersonal AS A INNER JOIN rb_reserve_servicepersonalproduct AS D ON A.ServiceType=D.ServiceType AND D.`Status`=0 AND D.ProductId IN({1})
WHERE A.Status=0 AND A.ServiceType=1
) AS A
LEFT JOIN
(
SELECT detailB.ServicepersonalId,COUNT(1) AS OrderNum