Jump to content

Recommended Posts

Hi.  I have MySQL 5.0.45.

 

My table looks kinda like this: (fake table, and i actually use real DATETIME, just too lazy to make fake ones for this example)

 

ID - Timestamp ------ IP --------- Browser

 

1  - today ------65.45.54.45------firefox 3.5.2

2 - yesterday---101.101.1.0-------IE 6.0

3 - last week----65.45.54.45 -----firefox 3.0.1

 

as you can see, the first and third entries have the same IP address.

 

i want my ip log to display the last 10 ip's logged, newest first (ORDER BY Timestamp DESC)

 

for the other information like Browser, I want pulled from the most Recent entry.

 

this is what I want the results to be like:

 

Today ------ 65.45.54.45 - 2 hits - firefox 3.5.2

Yesterday -- 101.101.1.0 - 1 hit -- IE 6.0

 

so... here's what I thought the query should be like:

 

SELECT ID, Timestamp, IP, Browser, COUNT(IP) GROUP BY IP ORDER BY Timestamp DESC

 

but that returns this:

 

Last Week -- 65.45.54.45 - 2 hits - firefox 3.0.1

Yesterday -- 101.101.1.0 - 1 hit --- IE 6.0

 

it groups up the IP's like it's supposed to, and COUNT(IP) returns the correct number of "hits" for that IP, but the other info for that IP are pulled from the oldest entry, which is the opposite of what I'm wanting, I'm wanting it to pull the Timestamp and Browser from the most recent hit.

 

I'm not sure that made sense but I hope my fake table and queries helped a little.

 

Thanks ahead of time.

Link to comment
https://forums.phpfreaks.com/topic/171608-need-help-ordering-a-group-by-query/
Share on other sites

Hi

 

Think you will have to do a JOIN with a subselect to get the max details.

 

SELECT a.ID, b.MaxTimestamp, b.IP, a.Browser, b.IpCount
FROM SomeTable a
INNER JOIN (SELECT IP, MAX(Timestamp) AS MaxTimestamp, COUNT(Timestamp) AS IpCount FROM SomeTable GROUP BY IP ORDER BY MaxTimestamp LIMIT 10) b
ON a.IP = b.IP AND a.Timestamp = b.MaxTimestamp

 

With a GROUP BY you should specify all the non aggregate fields that you are grouping on. If you don't then a random one will be returned (or with most flavours of SQL the SQL will not run).

 

All the best

 

Keith

 

 

Hi

 

Think you will have to do a JOIN with a subselect to get the max details.

 

SELECT a.ID, b.MaxTimestamp, b.IP, a.Browser, b.IpCount
FROM SomeTable a
INNER JOIN (SELECT IP, MAX(Timestamp) AS MaxTimestamp, COUNT(Timestamp) AS IpCount FROM SomeTable GROUP BY IP ORDER BY MaxTimestamp LIMIT 10) b
ON a.IP = b.IP AND a.Timestamp = b.MaxTimestamp

 

With a GROUP BY you should specify all the non aggregate fields that you are grouping on. If you don't then a random one will be returned (or with most flavours of SQL the SQL will not run).

 

All the best

 

Keith

cool thanks, i'll give it a shot.  that's alot more complex than i was thinking the solution would be.  i guess i should go finish reading my book.

 

thanks =)

Hi

 

It would be safer and faster if the ID field was in timestamp order . ID would be unique (assuming an autonumber) while strictly speaking the timestamp might not be.

 

All the best

 

Keith

so I guess I'd have to make ID auto increment backwards? is that even possible with just mysql?

 

i could use php to set the ID value so it decreases with each entry but that would require one extra query and a little more overhead. i'm guessing there's some mysql method of accomplishing this.

Hi

 

It might be possible, but don't think it is necessary. Just so long that the ID field is in timestamp order (assuming you haven't invented a time machine and so want to add events as they occur in reverse order  :psychic: ).

 

All the best

 

Keith

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.