通过左连接获取数据,每行包含count()

时间:2011-07-14 14:12:06

标签: php sql

我的代码:

$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
    ");

while($row = mysql_fetch_array($q))
{
    if($row['counted'] > $row['dclicks'])
    {
    }
    else
    {
        echo ''.$row['title_ad'].' '.$row['counted'].' '.$row['dclicks'].'<br>';
    }
}

我只想删除if(){}并想在sql查询中调整它。

解决方案是什么?怎么做?

1 个答案:

答案 0 :(得分:0)

在SQL中添加HAVING子句:

HAVING COUNT(adclicks.ad_id) < ads.dclicks