1

I am currently running a query that looks like this:

    SELECT campaigns.id, lead_stats.camp_id, lead_stats.country, campaigns.name, SUM(lead_stats.leads) AS lead, SUM(lead_stats.clicks) AS click,TRUNCATE(100.0 * SUM(lead_stats.leads) / SUM(lead_stats.clicks), 1) AS conv_rate
FROM lead_stats
JOIN campaigns
ON  campaigns.id = lead_stats.camp_id
WHERE lead_stats.date > DATE_SUB(NOW(), INTERVAL 24 HOUR)
AND lead_stats.from_product = 4
AND lead_stats.camp_id NOT IN (
  SELECT camp_id
  FROM suspicious_offer_clears
  WHERE clear_until > '".date('Y-m-d H:i:s')."'
  AND alert_type = 3
)
GROUP BY lead_stats.camp_id
HAVING (SUM(lead_stats.leads) / SUM(lead_stats.clicks)) < 0.1
ORDER BY lead_stats.camp_id

It gets some information that is not important really for my question that I have. However, I was curious on how I could, in this SQL statement, do an if statement on the lead_stats.country. I want to see if the country is NOT equal to "US", and if it is NOT equal to "US" I want to see if the SUM(lead_stats.click) for that row is greater than 20. I decided I would try to do it in my php template because I could not figure out how to do on MySQL.

Here is what I have on my template for that.

<?php
while ( $row = mysql_fetch_assoc($getConresult) ) {
  if($row['country'] != "US"){
    if($row['click'] > 19){
//not sure what to do here because it is less than 20, so I do not want to include this in my output on the table
  }
}

So I am wondering how I can skip a record if it does not match my criteria stated above. I was thinking I could use a for statement but had some confusion on that front. Any help would be appreciated!

Milan AM
  • 155
  • 1
  • 10
  • 2
    If you can, you should [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). They are no longer maintained and are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) [statements](http://php.net/manual/en/pdo.prepared-statements.php) instead, and consider using PDO, [it's really not hard](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard Jul 07 '15 at 19:31
  • 1
    Change to `if($row['click'] > 19) {` and add those rows. If you want to use `< 20` then put `continue;` in the `if` condition and the table output in the `else` condition. – Jay Blanchard Jul 07 '15 at 19:32
  • Jay, that makes sense, but what if I also want to include those that ARE equal to "US" but not worry about the 20 click threshold on those. Or would they be included because pass the first if statement? Also I know they are deprecated, but am using them just for this project. – Milan AM Jul 07 '15 at 19:36
  • They should be included. – Jay Blanchard Jul 07 '15 at 19:38

1 Answers1

1

Because it's a while loop, you should just skip that iteration

while ( $row = mysql_fetch_assoc($getConresult) ) {
  if($row['country'] != "US"){
    if($row['click'] < 20){
     continue; //skip to the next loop
  }
}

If you need to stop the loop, use break instead of continue

Machavity
  • 30,841
  • 27
  • 92
  • 100