php - How can I convert these two queries in a loop into a single JOINed query? -
i trying data table (mostkills weapon in table on 300 kills). did normal query
$q = $mysql->query("select * `kills`") or die($mysql->error);
but when tried
$query2 = $mysql->query("select `killerid`, count(`killerid`) tot_kills `kills` `killtext` '%$gun%' group `killerid` order `tot_kills` desc;") or die($mysql->error); $kdata = $query2->fetch_assoc(); $query3 = $mysql->query("select `username` `players` `id` = '" . $kdata['killerid'] . "'") or die($mysql->error); $udata = $query3->fetch_assoc(); $array[$gun]['kills']++; $array[$gun]['gun'] = $gun; $array[$gun]['bestkiller'] = $udata['username']; $array[$gun]['killamount'] = $kdata['tot_kills']; function sortbykills($a, $b) { return $b['kills'] - $a['kills']; } usort($array, 'sortbykills'); foreach($array $i => $value) { // table here }
i had in while loop, caused there around 600 queries, , not acceptable. have tips on how can optimize this, or turn single query?
i heared join this, don't know it, , wondering if guys me
try this...
i added inner join
, added username
select clause. min()
way include username
column in select
, not have impact on result long have 1 username every killerid
select `killerid` , count(`killerid`) tot_kills , min(`username`) username `kills` inner join `players` on `players`.`id` = `kills`.`killerid` `killtext` '%$gun%' group `killerid` order `tot_kills` desc
Comments
Post a Comment