get data by left join with count() for each row
My code:
$q = mysql_query("
SELECT
ads.title_ad AS title_ad,
COUNT(adclicks.ad_id) AS counted,
ads.dclicks AS dclicks
FROM ads
LEFT JOIN adclicks
ON ads.id = adclicks.ad_id GROUP BY ads.id
开发者_JAVA百科 ");
while($row = mysql_fetch_array($q))
{
if($row['counted'] > $row['dclicks'])
{
}
else
{
echo ''.$row['title_ad'].' '.$row['counted'].' '.$row['dclicks'].'<br>';
}
}
I just want to remove if(){}
and want to adjust it in sql query.
Whats the solution? How to do that?
Add a HAVING clause to your SQL:
HAVING COUNT(adclicks.ad_id) < ads.dclicks
精彩评论