Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 252003
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T21:38:31+00:00 2026-05-11T21:38:31+00:00

I have a table containing pagehit (normalized) data and I need to grab the

  • 0

I have a table containing pagehit (normalized) data and I need to grab the 10 latest unique ips.

I tried to do it like this:

SELECT * FROM spy_hits ORDER BY date desc GROUP BY ip LIMIT 10;

Which should give me this result:

+-----+------------+-----+---------+----+------+------+---------+-------+-------+
| id  | date       | ip  | browser | os | page | host | referer | query | agent |
+-----+------------+-----+---------+----+------+------+---------+-------+-------+
| 354 | 1244442065 |   2 |       3 |  2 |   16 |    1 |      47 |    12 |     2 |
| 311 | 1244442000 |   1 |       2 |  1 |   16 |    1 |      36 |    12 |     1 |
+-----+------------+-----+---------+----+------+------+---------+-------+-------+
2 rows in set (0.00 sec)

That is the latest unique visitors to the site.

But I get a syntax error instead of that result.

So I have to do this query:

SELECT * FROM spy_hits GROUP BY ip ORDER BY date desc LIMIT 10;

Which I thought would be ok. But it gives this result:

+-----+------------+-----+---------+----+------+------+---------+-------+-------+
| id  | date       | ip  | browser | os | page | host | referer | query | agent |
+-----+------------+-----+---------+----+------+------+---------+-------+-------+
| 280 | 1242130841 |   2 |       3 |  2 |   16 |    1 |      47 |    12 |     2 |
| 268 | 1242130818 |   1 |       2 |  1 |   16 |    1 |      36 |    12 |     1 |
+-----+------------+-----+---------+----+------+------+---------+-------+-------+
2 rows in set (0.00 sec)

But that sets order by date after its grouped already so it grabs the first unique ips in the table which are also the oldest.

So the table goes:

 id --- date
 268    1242130818    (Old)
  |      |
  V      V
 354    1244442065    (New)

But I want it to go like this before I do the group by:

 id --- date
 354    1244442065    (New)
  ^       ^
  |       |
 268    1242130818    (Old)

I’m using PHP with it so if anyone has an idea of how to get the results with a PHP solution.

Cheers in advance 🙂

  • 1 1 Answer
  • 4 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T21:38:31+00:00Added an answer on May 11, 2026 at 9:38 pm

    If you have few DISTINCT IP‘s:

    SELECT  ip, MAX(date) AS maxdate
    FROM    (
            SELECT  ip, MAX(date) AS maxdate
            FROM    spy_hits
            GROUP BY
                    ip
            )
    ORDER BY
            maxdate DESC
    LIMIT 10
    

    If you have lots of DISTINCT IP‘s:

    SELECT  *
    FROM    spy_hits so
    WHERE   NOT EXISTS
            (
            SELECT  1
            FROM    spy_hits si
            WHERE   si.ip = so.ip
                    AND si.date > so.date
            )
    ORDER BY
            date DESC
    LIMIT 10
    

    Creating two indexes on this table:

    CREATE INDEX ix_spyhits_date ON spy_hits (date)
    CREATE INDEX ix_spyhits_ip_date ON spy_hits (ip, date)
    

    will improve these queries a lot.

    See this entry in my blog for performance details:

    • Latest DISTINCT records
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table containing millions of transaction records(Obj1) which looks like this TransactionNum
I have a table containing data about some users. Many of them use our
I have a table containing reports and the date/time they were created. I'd like
I have a Table containing some information that I need. All these rows also
Hi i have a table structure like this. table **geoinfo** id name desc I
I have a table containing the following data periodID periostart periodend 1 01-01-2012 10-01-2012
I have a table containing numbers, range between 1-1000 they are unique, but I
I have a table containing a unique ID field. Another field (REF) contains a
I have a table containing 2 entries. Something like CREATE TABLE `db`.`main` ( `id`
I have this table containing 3 radio buttons, I want to find out whether

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.