mysql - Complex query using sql in php -
i have question,
i need create complex select query , can't :
so have multiples databases call cluster1,cluster2,cluster3,...,cluster10,
every cluster have table call games. , need of rows clusters in last 7 days. tried this:
foreach ($adatabases $database) { $sflashparties = sprintf(' select left(depart, 10) date, count(*) nb `%s` duree>0 , depart between current_date()-7 , current_date() group date', $sflashtablename); $r_count = mysqli::query($sflashparties, $database); $a_row = $r_count->fetch_array(mysqli_assoc); error_log(print_r($a_row,true), 3, "/tmp/error.log"); }
the result need :
day nb_total
can me please? thx in advance
given single connection host multiple databases, build , execute query string below.
note using backtick operator indicate database.table
location.
edit: taking first result each query, fix doing this:
<?php $databases = array("cluster1","cluster2","cluster3","cluster10"); foreach ($databases $db) { $sflashtablename = '`' . $db . '`.`games`'; print sprintf('select left(depart, 10) date, count(*) nb %s duree>0 , depart between date_sub(curdate(), interval 7 day) , curdate() group date', $sflashtablename) . php_eol; // execute query in loop , store results in array // parse array later , aggregate results // $totals[] = queryresult; $r_count = mysqli::query($sflashparties, $database); while($a_row = $r_count->fetch_array(mysqli_assoc);){ $rows[] = $row; // add every result line databases 1 single array $rows[$$db][] = $row; // not tested!, should give similar $rows[cluster2] = array($a_row['date'],$a_row['nb']); } } /* select left(depart, 10) date, count(*) nb `cluster1`.`games` duree>0 , depart between date_sub(curdate(), interval 7 day) , curdate() group date select left(depart, 10) date, count(*) nb `cluster2`.`games` duree>0 , depart between date_sub(curdate(), interval 7 day) , curdate() group date select left(depart, 10) date, count(*) nb `cluster3`.`games` duree>0 , depart between date_sub(curdate(), interval 7 day) , curdate() group date select left(depart, 10) date, count(*) nb `cluster10`.`games` duree>0 , depart between date_sub(curdate(), interval 7 day) , curdate() group date */
you can choose results next.
Comments
Post a Comment