Материал из BiTel WiKi
Темы на форуме
SELECT c.id,c.title,c.comment, sum(REPLACE(DATA,'type&1%cost&','')) AS abon
FROM contract c
LEFT JOIN contract_balance b ON c.id=b.cid
LEFT JOIN contract_tariff AS t ON c.id=t.cid AND t.date2 IS NULL
LEFT JOIN tariff_plan AS p ON t.tpid=p.id
LEFT JOIN tariff_tree_link ON tariff_tree_link.tpid=p.id
LEFT JOIN module_tariff_tree ON module_tariff_tree.tree_id=tariff_tree_link.tree_id
LEFT JOIN mtree_node ON mtree_node.mtree_id=module_tariff_tree.id
WHERE b.yy=YEAR(NOW()) AND b.mm=MONTH(NOW()) AND mtree_node.type='month_cost'
GROUP BY c.id