On my website, I want to use a lot of different data from my database. Currently, I'm using four queries to gather different data. But is there a way to make it more efficient and put them into one big query? And how would I do that?
Edit: So the answer was to simply put all queries together into one and use as much data manipulation as possible in the database queries, and not in php.
$qry = "SELECT COUNT(*) cnt,
AVG(level) avg_lvl,
SUM(IF(onlinestatus=1, 1, 0)) online_cnt,
(SELECT Max(time) FROM refreshes) refresh_time
FROM players";
foreach ($db->query($qry) as $row){
$amount_total = $row['cnt'];
$average_level = floor($row['avg_lvl']);
$online_amount = $row['online_cnt'];
$milliseconds = $row['refresh_time'] + 1800000;
$update_time = DateTime::createFromFormat('U', intval($milliseconds / 1000));
}
rookstayersandrefreshestables then you could create the sql as aleft outer joinbetween the tables and do what you want with one query. Without that it looks like this could be done in 2 queries becauseselect * from rookstayersshould be enough to manipulate the data nad perform whatever calculations you want in phpSELECT * FROM rookstayerson the first 3 queries? Then one forrefreshes? But the thing is, I also have some stuff likeWHERE something = somethingwouldn't it screw up the query for the ones that doesnt use WHERE?. You got any example on aleft outer join?refreshestable dataforeach; there will be exactly one row returned.