select bhv.id,count(1) as Bound from bst_hardware_version bhv left join bst_device bd on bhv.id = bd.hardware_version_id where bd.mch_id is not null group by bhv.id select bhv.id,count(1) as unBound from bst_hardware_version bhv left join bst_device bd on bd.hardware_version_id = bhv.id where bd.mch_id is null group by bhv.id