php - How can I count the total number of duplicate values between 2 tables on an inner join -
i have following query
select fixtures.fixture_id, fixtures.home_score, fixtures.away_score, predict.fixture_id, predict.phome_score, predict.paway_score fixtures inner join predict on fixtures.fixture_id=predict.fixture_id
i want count number of times following condition met
fixtures.home_score=predict.phome_score , fixtures.away_score=predict.paway_score , fixtures.fixture_id=predict.fixture_id
i tried using 'count()' 'having count()>1' cant syntax work
i have tried count number of times if condition met in following php. i'm not sure if possible, thought count might have done within sql statement
<?php $current = $user->data()->id; $sql2 = "select fixtures.home_team, fixtures.away_team, fixtures.home_score, fixtures.away_score, predict.phome_score, predict.paway_score fixtures inner join predict on fixtures.fixture_id=predict.fixture_id predict.id='".$current."'"; echo "the number of detected predictions:", '<br>'; $predictions = db::getinstance()->query($sql2); foreach ($predictions->results() $rows) { $rows= get_object_vars($rows); $num_rows= $predictions->count(); } for($count=0;$count<$num_rows;$count++){ $r_home_score = $predictions->results()[$count]->home_score; $p_home_score = $predictions->results()[$count]->phome_score; $r_away_score = $predictions->results()[$count]->away_score; $p_away_score = $predictions->results()[$count]->paway_score; $p=0; if($r_home_score==$p_home_score&&$r_away_score==$p_away_score){ $p++; echo $p; } }
the output is:
the number of detected predictions: 111111
i want output 6
if want single count returned database, query this:
select count(*) mycount fixtures f join predict p on p.fixture_id = f.fixture_id , p.phome_score = f.home_score , p.paway_score = f.away_score
followup
$dbh = db::getinstance(); $sql = "select count(*) mycount fixtures f join predict p on p.fixture_id = f.fixture_id , p.phome_score = f.home_score , p.paway_score = f.away_score p.id = ?"; if ($sth = $dbh->prepare($sql)) { $sth->bindparam(1, $current, pdo::param_int); if ($sth->execute()) { if ($row = $sth->fetch(pdo::fetch_assoc)) { echo $row['mycount']; } else { // should never happen count(*) query echo "query returned 0 rows"; } } else { echo "pdo error on execute: "; print_r($dbh->errorinfo()); } else { echo "pdo error on prepare: "; print_r($dbh->errorinfo()); }
Comments
Post a Comment