Can I use max and minimums for row results ?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jeddiki
    Contributor
    • Jan 2009
    • 290

    Can I use max and minimums for row results ?

    Hi
    I have got my query close to what I need but I want to try and include a couple of other conditions:

    I want the result to contain:
    a minimum of 2 rows that contain the same cb_id (but different ip_adr).
    and
    a maximum of 4 rows that contain the same cb_id (but different ip_adr).

    So if the result from this query:

    Code:
    $sql = "SELECT distinct ip_adr, cb_id FROM cb_promo WHERE link_dt BETWEEN '$days7ago' and '$today' ORDER BY cb_id";
     $result = mysql_query($sql)	or die("could not find PROMO". mysql_error());  
     while($row = mysql_fetch_assoc($result)){
     
     $cb_promo_id = $row['cb_id'];
     $ipadd = $row['ip_adr'];
    echo "<br> ID: $cb_promo_id, IP add: $ipadd";
     }
    would have been:

    ID: EBFORTUNE, IP add: 55.167.889.182
    ID: EBFORTUNE, IP add: 78.167.109.182
    ID: FEDYDE, IP add: 85.106.68.197
    ID: FEDYDE, IP add: 85.176.68.227
    ID: FEDYDE, IP add: 89.106.68.127
    ID: FHHHH45, IP add: 78.167.109.182 // only one - not enough
    ID: P97GFRH, IP add: 56.107.9.182 // only one - not enough
    ID: RSEDE38M, IP add: 78.167.17.182
    ID: RSEDE38M, IP add: 78.167.109.182
    ID: RSEDE38M, IP add: 23.167.109.182 // 7 the extra 3 rows must be filtered out
    ID: RSEDE38M, IP add: 78.16.143.182
    ID: RSEDE38M, IP add: 78.19.109.102
    ID: GGG72FHH, IP add: 178.7.109.18 // only one - not enough
    ID: RSEDE38M, IP add: 17.167.19.2
    ID: RSEDE38M, IP add: 712.17.39.182


    Then the result I want to get is (min 2, maz 4):

    ID: EBFORTUNE, IP add: 55.167.889.182
    ID: EBFORTUNE, IP add: 78.167.109.182
    ID: FEDYDE, IP add: 85.106.68.197
    ID: FEDYDE, IP add: 85.176.68.227
    ID: FEDYDE, IP add: 89.106.68.127
    ID: RSEDE38M, IP add: 78.167.109.182
    ID: RSEDE38M, IP add: 78.167.109.182
    ID: RSEDE38M, IP add: 78.167.109.182
    ID: RSEDE38M, IP add: 78.167.109.182

    Is there a way to put this into my query
    or do I need to use if's to walk through the result array ?

    Thanks for any advice :)
  • jeddiki
    Contributor
    • Jan 2009
    • 290

    #2
    Maybe I should show where I am getting
    the date range values from.

    The data gets timestamped as in goes into the
    table with $today

    and $today = date("U");

    so:

    Code:
    $today = date("U");
    $days7ago = $today-604800;
    
    $sql = "SELECT distinct ip_adr, cb_id FROM cb_promo WHERE link_dt BETWEEN '$days7ago' and '$today' ORDER BY cb_id";
     $result = mysql_query($sql)    or die("could not find PROMO". mysql_error());  
     while($row = mysql_fetch_assoc($result)){
     
     $cb_promo_id = $row['cb_id'];
     $ipadd = $row['ip_adr'];
    echo "<br> ID: $cb_promo_id, IP add: $ipadd";
    Hope that makes it clearer.
    Would appreciate any help from someone who knows how I can add the
    extra conditions that I mentioned in my first post.

    Comment

    Working...