MySQL/PHP merge rows by row values -
i need little mysql select in sql or php. situation: have sql table this
id | organization_id | investment | investment_curr | paid | paid_curr | effective_from | effective_to 1 | 195001 | 5000 | eur | null | eur | 2010-06-23 | null 2 | 195001 | null | eur | 5000 | eur | 2010-06-23 | null 3 | 195001 | 2000 | skk | null | skk | 2007-08-01 | 2010-06-22 4 | 195001 | null | skk | 2000 | skk | 2007-08-01 | 2010-06-22
now have php select (joomla), when select values organization_id
$query_rpo_organization_equity_entries = $db->getquery(true); $query_rpo_organization_equity_entries ->select(array('organization_id', 'investment_amount', 'investment_currency', 'paid_amount', 'paid_currency', 'effective_from', 'effective_to' )) ->from($db->quotename('rpo_organization_equity_entries')) ->where($db->quotename('organization_id') . ' = '. $db->quote($rpo_id)) ->order(($db->quotename('effective_to') . ' not null'), $db->quotename('effective_to') . ' desc '); $db->setquery($query_rpo_organization_equity_entries); $query_rpo_organization_equity_entries = $db->loadobjectlist();
then im calling foreach
foreach($query_rpo_organization_equity_entries $rpo_organization_equity_entries) {}
everything works right, not way need it. foreach
returns 4 arrays, need merge/combine rows columns "effective_from" , "effective_to" (if have same values. , organization_id ofcourse). combine row ids 1 , 2, , rows ids 3 , 4, , instead of 4 arrays have 2 combine/merge arrays this
xy | 195001 | 5000 | eur | 5000 | eur | 2010-06-23 | null xy | 195001 | 2000 | skk | 2000 | skk | 2007-08-01 | 2010-06-22
how should it? better solve sql , in php?
thx
the result wish can achieved mysql using group by function.
in joomla docs pretty clear on one. van use group() function on object.
//example $query ->select( array('catid', 'count(*)') ) ->from($db->quotename('#__content')) ->group($db->quotename('catid'));
in case want change code like:
$query_rpo_organization_equity_entries = $db->getquery(true); $query_rpo_organization_equity_entries ->select(array('organization_id', 'max(investment_amount)', 'investment_currency', 'max(paid_amount)', 'paid_currency', 'max(effective_from)', 'max(effective_to)' )) ->from($db->quotename('rpo_organization_equity_entries')) ->where($db->quotename('organization_id') . ' = '. $db->quote($rpo_id)) ->group('`organization_id`, `effective_from`, `effective_to`') //this 1 ->order(($db->quotename('effective_to') . ' not null'), $db->quotename('effective_to') . ' desc '); $db->setquery($query_rpo_organization_equity_entries); $query_rpo_organization_equity_entries = $db->loadobjectlist();
i have not tested in lines of should work.
Comments
Post a Comment