/    Sign up×
Community /Pin to ProfileBookmark

How 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 would like is 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).

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

and [B]$today = date(“U”);[/B]

So if the result from this query::

[code=php]
$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”;

[/code]

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 sql query or
would it be easier / more efficient to do it with php
after the query ( or maybe use a second query ) ?

Thanks for any advice ?

to post a comment
PHP

0Be the first to comment 😎

×

Success!

Help @jeddik spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...